UNIX command tricks
Some of the day to day UNIX commands used by a Basis admin have been covered by one of my previous posts. Here are some tips and tricks you might find usable.
In this example, I am trying to list a file, but I typed the file name wrong:
sapnwnewbie:user1 2> ls -l i-made-a-typo
i-made-a-typo not found
Now I correct the typo, and typo alone, using ^ in front of replacement and error. The corrected command runs automatically.
sapnwnewbie:user1 3> ^i-made-a-typo^startsap.trc
ls -l startsap.trc
-rw-r--r-- 1 user1 group1 33155 Apr 17 03:03 startsap.trc
In this example, I have copied a file to a different directory. I will reuse the last argument in the command (the directory path)
sapnwnewbie:user1 4> pwd
/home/user1
sapnwnewbie:user1 5> cp test_delete_this_file /usr/sap/DEV/DVEBMGS01
Now I will reuse /usr/sap/DEV/DVEBMGS01, which is stored in !$
sapnwnewbie:user1 6> cd !$
cd /usr/sap/DEV/DVEBMGS01
sapnwnewbie:user1 7> pwd
/usr/sap/DEV/DVEBMGS01
Execute the command again, by replacing the last argument
This runs against the use case of the previous trick. This time we will use everything from the previous command, except the last argument.
I have copied a file to a particular location
sapnwnewbie:user1 78> cp remove_this_file /usr/sap/DEV
Now I want to copy the came file to a different location. So the command is the same one except for the last argument. The command, sans last argument, is stored in !:-
sapnwnewbie:user1 79> !:- /usr/sap/DEV/DVEBMGS01
cp remove_this_file /usr/sap/DEV/DVEBMGS01
In this example, I will change to a new directory location (cd /usr/sap/DEV). Run a command from the new directory location (ls -ltr) and when done I will be back to the directory where I started off (/home/user1). The && is an AND operator. In this example ls -ltr will run only if cd to /usr/sap/DEV was successful. If changing to new directory failed, the command execution will return a false, and false ANDed with true/false is false. Therefore the shell won't bother executing ls -ltr.
sapnwnewbie:user1 21> (cd /usr/sap/DEV && ls -ltr)
total 1224
drwxr-xr-x 2 root sys 96 Jun 29 2006 global
drwxr-xr-x 5 user1 sapsys 96 Jan 31 2016 SYS
drwxr-xr-x 7 user1 sapsys 96 Jan 31 2016 SCS00
drwxr-xr-x 2 user1 sapsys 96 Jan 31 2016 put
drwxr-xr-x 2 user1 sapsys 96 Jan 31 2016 config
drwxr-xr-x 9 user1 sapsys 1024 Apr 19 09:35 DVEBMGS01
sapnwnewbie:user1 22> pwd
/home/user1
In this example, I have a file named testfile.old and I am renaming it to testfile.new.
sapnwnewbie:user1 39> ls -l testfile.old
-rw-r--r-- 1 user1 group1 0 Apr 19 10:08 testfile.old
sapnwnewbie:user1 40> mv testfile.{old,new}
sapnwnewbie:user1 41> ls -l testfile.old
testfile.old not found
sapnwnewbie:user1 42> ls -l testfile.new
-rw-r--r-- 1 user1 group1 0 Apr 19 10:08 testfile.new
I will use similar trick to take a backup of a file.
sapnwnewbie:user1 121> cp remove_this_file{,.bck}
sapnwnewbie:user1 122> ls -l remove_this_file*
-rw-r--r-- 1 user1 group1 0 Apr 19 10:23 remove_this_file
-rw-r--r-- 1 user1 group1 0 Apr 19 11:41 remove_this_file.bck
There are some more tricks. I'll post them later.
Replace a string from previous command and execute
If you have run a command with an incorrect argument (usually a typo), you need not retype the entire command. You can use this trick to replace the typo and run the command.In this example, I am trying to list a file, but I typed the file name wrong:
sapnwnewbie:user1 2> ls -l i-made-a-typo
i-made-a-typo not found
Now I correct the typo, and typo alone, using ^ in front of replacement and error. The corrected command runs automatically.
sapnwnewbie:user1 3> ^i-made-a-typo^startsap.trc
ls -l startsap.trc
-rw-r--r-- 1 user1 group1 33155 Apr 17 03:03 startsap.trc
Reusing last argument from the previous command
This is useful if you have used an argument in a command and if you need to use it again this time. Only the last argument can be reused in this trick; but that is quite a common occurrence in a real life case.In this example, I have copied a file to a different directory. I will reuse the last argument in the command (the directory path)
sapnwnewbie:user1 4> pwd
/home/user1
sapnwnewbie:user1 5> cp test_delete_this_file /usr/sap/DEV/DVEBMGS01
Now I will reuse /usr/sap/DEV/DVEBMGS01, which is stored in !$
sapnwnewbie:user1 6> cd !$
cd /usr/sap/DEV/DVEBMGS01
sapnwnewbie:user1 7> pwd
/usr/sap/DEV/DVEBMGS01
Execute the command again, by replacing the last argument
This runs against the use case of the previous trick. This time we will use everything from the previous command, except the last argument.
I have copied a file to a particular location
sapnwnewbie:user1 78> cp remove_this_file /usr/sap/DEV
Now I want to copy the came file to a different location. So the command is the same one except for the last argument. The command, sans last argument, is stored in !:-
sapnwnewbie:user1 79> !:- /usr/sap/DEV/DVEBMGS01
cp remove_this_file /usr/sap/DEV/DVEBMGS01
Jump to a directory, execute a command and jump back to current directory
All I need to do is && operator and type the command in parenthesis ().In this example, I will change to a new directory location (cd /usr/sap/DEV). Run a command from the new directory location (ls -ltr) and when done I will be back to the directory where I started off (/home/user1). The && is an AND operator. In this example ls -ltr will run only if cd to /usr/sap/DEV was successful. If changing to new directory failed, the command execution will return a false, and false ANDed with true/false is false. Therefore the shell won't bother executing ls -ltr.
sapnwnewbie:user1 21> (cd /usr/sap/DEV && ls -ltr)
total 1224
drwxr-xr-x 2 root sys 96 Jun 29 2006 global
drwxr-xr-x 5 user1 sapsys 96 Jan 31 2016 SYS
drwxr-xr-x 7 user1 sapsys 96 Jan 31 2016 SCS00
drwxr-xr-x 2 user1 sapsys 96 Jan 31 2016 put
drwxr-xr-x 2 user1 sapsys 96 Jan 31 2016 config
drwxr-xr-x 9 user1 sapsys 1024 Apr 19 09:35 DVEBMGS01
sapnwnewbie:user1 22> pwd
/home/user1
Rename a file or take a backup
When renaming a file name or taking a backup of a file, if you are using the most part of the file name, you can avoid typing it in twice. The changing part is specified in {}.In this example, I have a file named testfile.old and I am renaming it to testfile.new.
sapnwnewbie:user1 39> ls -l testfile.old
-rw-r--r-- 1 user1 group1 0 Apr 19 10:08 testfile.old
sapnwnewbie:user1 40> mv testfile.{old,new}
sapnwnewbie:user1 41> ls -l testfile.old
testfile.old not found
sapnwnewbie:user1 42> ls -l testfile.new
-rw-r--r-- 1 user1 group1 0 Apr 19 10:08 testfile.new
I will use similar trick to take a backup of a file.
sapnwnewbie:user1 121> cp remove_this_file{,.bck}
sapnwnewbie:user1 122> ls -l remove_this_file*
-rw-r--r-- 1 user1 group1 0 Apr 19 10:23 remove_this_file
-rw-r--r-- 1 user1 group1 0 Apr 19 11:41 remove_this_file.bck
There are some more tricks. I'll post them later.
Comments
Post a Comment