Linux Linux Tips 2 - ctrl a, crl r, tail -f, umask, and eval
When we find a typo error in our command, and we want to go back the the beginning of the command, we should use ctrl-a instead of scrolling via left arrow. To go to the end of a command, we should use ctrl e:
To display the first 10 lines:
$ head foo.txt
The last 10 lines:
$ tail foo.txt
Use -n option to specify the number of lines to display:
$ tail -n5 foo.txt # the last 5 lines $ head -n5 foo.txt # the first 5 lines
'+' in tail and '-' in head:
$ tail -n+10 foo.txt # start from the 10th line to the last $ head -n-10 foo.txt # print all, except the last 10 lines
Keep watching tail:
$ tail -f foo.log
Instead of using up or down arrow to view the command issued, we can use ctrl r + command key stroke to retrieve the previous commands. For example, suppose the following is the list of the recent commands:
$ command 1 $ command 2 $ command 3 $ command 4 $ command 5
If we hit cntr r, and type in "c", we get:
(reverse-i-search)`c': command 5
If that's what we want, just hit 'Return'. Then command 5" will be the current command:
$ command 5If now, we can search in reverse by keep typing in cntr r.
Default permissions are assigned by the system whenever we create a new file or directory, and these are governed by the umask setting. The 'mask' serves to remove permissions as opposed to granting them.
That is, the digits in the umask number are 'subtracted' from 777 for directories or 666 for files when we are creating their initial permissions. Why 666 is the maximum value for a file? This is because only scripts and binaries should have execute permissions, normal and regular files should have just read and write permissions. Directories require execute permissions for viewing the contents in it, so they can have 777 as permissions.
Suppose we type:
% umask 022
Then when we create new files their default permissions will be 644 (666 minus 022, i.e. -rw-r--r--). When we create new directories their default permissions will be 755 (drwxr-xr-x). If the umask value were instead set to 077, our default permissions would be 600 (-rw-------) and our default directory permissions would be 700 (drwx------).
We can check what umask we're using:
$ umask 0002
Note: If USERGROUPS_ENAB is set to "yes", that will modify this UMASK default value for private user groups, i. e. the uid is the same as gid, and username is the same as the primary group name: for these, the user permissions will be used as group permissions, e. g. 022 will become 002 - /etc/login.defs
The '>' is used for redirection from the left to right side of '>'. The format looks like this:
input > output
Here is an example:
$ echo "pushing text" > myfile.txt $ cat myfile.txt pushing text
File descriptors are the places a program sends its output to and gets its input from. They are shared with user space, and are used directly by user programs to access files. When we execute a program, Linux opens three file descriptors for the program:
0 standard input (stdin) 1 standard output (stdout) 2 standard error (stderr)
The redirect output symbol ( >) is shorthand for 1>, which tells the shell to redirect standard output. Similarly, < is short for 0<, which redirects standard input. The symbol 2> redirects standard error.
So, the previous example can be like this:
$ echo "pushing text" 1> myfile.txt $ cat myfile.txt pushing text
When we redirect output using '>', since the '1' in '1>' is a default output file descriptor, we can drop it.
How about the error redirection?
$ cat nonExistingFile > error.out cat: nonExistingFile: No such file or directory $ cat error.out $
As we can see, nothing's been written to the file 'error.out'. Though the message printed out looks similar to stdout, it's different kind. It's an error. So, if we want to save the stderr message, we should use file descriptor for error which is '2':
$ cat nonExistingFile 2> error.out $ cat error.out cat: nonExistingFile: No such file or directory
Now the error message has been written to 'error.out' file.
How about this one?
$ telnet dummy.com Trying 5.22.149.135... telnet: Unable to connect to remote host: Connection refused $ telnet dummy.com 2> error.out Trying 69.43.160.154... $ cat error.out telnet: Unable to connect to remote host: Connection refused
We captured the stderr successfully. What if we want to get both? In other words, stderr and stdout together. We use &>:
$ telnet dummy.com &> error.out $ cat error.out telnet: Unable to connect to remote host: Connection refused Trying 69.43.160.154...
There is another way to redirect both the stdout and stderr though this is an old way of doing that. We use '2>&1'
$ telnet dummy.com > error.out 2>&1 $ cat errot.out telnet: Unable to connect to remote host: Connection refused Trying 69.43.160.154...
We hate errors, so let's try throw them away:
$ telnet dummy.com > error.out 2> /dev/null
The '<' is used for std input redirection. In the following example, the 'grep' is used to find a string from the given input:
$ grep a < input.txt abc abba
where the 'input.txt' looks like this:
$ cat input.txt abc def abba 1234
eval
command is used to execute arguments like a shell command. Arguments are joined in a string and taken as input for the shell command to execute the command.
$ d = "date" -bash: d: command not found $ d="date" $ echo $d d $ eval $d Fri Aug 14 16:03:08 PDT 2020 $ d2="date +%Y-%h-%d" $ echo $d2 date +%Y-%h-%d $ eval $d2 2020-Aug-14
$ minikube docker-env export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.64.7:2376" export DOCKER_CERT_PATH="/Users/kihyuckhong/.minikube/certs" export MINIKUBE_ACTIVE_DOCKERD="minikube" # To point your shell to minikube's docker-daemon, run: # eval $(minikube -p minikube docker-env) $ eval $(minikube docker-env) $ echo $DOCKER_HOST tcp://192.168.64.7:2376
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization