You are on page 1of 12

11.

' mkdir' - creates new directories

a. 'mkdir temp'

We can also create multiple Directories like below

12. 'rm' - removes file(s)/directory(ies) - removes recursively

a. 'rm -rf temp*'

This is to remove directories/files recursively from the pwd

b. 'rm -rf temp[34]' - removes a range of items using Regular Expression (RegEx) - Character-Class
To remove the selected directories we use the following cmmd , this cmmd remove only temp3 and
temp 4 directory not temp2

You can use character class with any linux cmd here we list the temp 3 & 4 files content.

13. 'which' - searches current $PATH for executable

a. 'which cat' && 'which ls'

for ‘ls’ it is showing the alias , alias means the second name of cmd the primary cmd generally have
options associated with the cmd like ls given below here option is color
14. 'echo $PATH' - reveals the current $PATH for the executables

As you can see that these are the path for the user linuxcbt , path are delimited with : and when you
search for the path it will start finding the path from left to right in the paths given for the user and path
may differ according to the users.

15. Redirection:

a. '<' - INPUT - Usually defaults to a source file

b. '>' - OUTPUT - clobbers target file

c. '>>' - APPEND - appends to target file if it exists and creates it if it doesn't :** studied here only

Examples:

a. 'cat test.txt' - reads the file 'test.txt' as STDIN (Standard INPUT)

Note: However, most commands will wait for keyboard input if no input file is specified

i.e. b. 'cat ' - waits on STDIN for input

Note: Use: 'CTRL-D' to quit STDIN from keyboard

Note: 'cat -' does the same as: 'cat'

b. 'cat test.txt > helloworld.txt' - bypasses STDOUT (Standard OUTPUT)


c. 'cat test.txt >> helloworld.txt' - "but APPENDS to target file”

16. Linux | UNIX Pipes - connects output stream of command a to input stream of command b

a. 'cat /var/log/messages | less' - pipes output of 'cat...' into 'less' (/var/log/messages : this is the main
system log file)

b. 'cat /var/log/messages | grep kernel | less' - parses '/var/log/messages' for keyword 'kernel' then
pipes the output to 'less' to display one pageful at a time

Note: When piping, STDIN becomes the content of the pipe

17. Command Chaining

a. 'cat /var/log/messages | grep kernel | wc -l'

b. 'rm -rf temp* ; ls -l' - runs both commands independently in Linux ‘;’ means the next line command
to execute

Note: Command Chaining is not dependent upon the exit status of the most-recently executed
command

18. Command Dependency: AND || OR


a. 'rm -rf temp* || ls -l' - run 'ls -l' if 'rm -rf temp*' fails

by the OR Condition means that ls –l will run only if rm –rf fails so as we see the output rm –rf executed
that is why we didn’t get the ls –l output.

b. 'rm -rf temp* && ls -l' - run 'ls -l' if and only if 'rm -rf temp*' works

It will create the logical dependency so ls –l only run if rm –rf will get executed.
As you can see that in below condition we have logical AND if the initial cmd is not coreect then all other
cmds in the chain will not executed , so what it means that execution of any cmd in cmd chaining will
depend on the the correct execution of previous cmd.

Here we are using the OR Condition in the below PS it shows that first we failed pwd by typing pwds so
ls executed then we type pwds and lss to make these both cmd fail the ‘ps’ executed

19. Command History - built-in command (BASH) that means it is within bash intrepreter as
aconsequence there is no file on the disk that corelate with this cmd but it is available at your disposal
courtesy of shell

a. 'history'

bash shell maintain the history of all commands on per user basis
By running history cmd it will shows are cmds which shows list of all cmds that was run on this shell the
history itme is maintained in the home directory ‘.bash_history’ note that we write this ls cmd like

Ls .bash_history -l some of these cmds are permitted on the bash shell

Here cat shows the history of cmds from the first tty shell
Here you saw the hidden file like .bash_history etc

Note: BASH maintains a number of variables per shell

a. 'OLDPWD' - updated as you navigate the directory tree

b. 'LOGNAME'

c. 'SHELL'
etc.
‘set’ cmd will also the variables

20. 'export' exports vars

By export cmd we can add new variable in the PATH

Note : export of this variable ‘/tmp’ is specific to this shell only if you check on the other shell it will not
show this variable.

a. 'export PATH=$PATH:/tmp' -appends '/tmp' to current shell's PATH


21. 'more' - similar to 'less'

22. 'cp' - copies data

a. 'mkdir temp && cp -v test.txt temp/ && ls -l temp/'

23. 'mv' - moves data

a. 'mv test.txt temp/ && ls -l . && ls -l temp/ && echo $?'


Note: In scripts, prefix exit status with meaningful text:

i.e. 'echo "EXIT STATUS: " $? '

Note: BASH Shell allows simple navigation using:

a. 'CTRL-a' - takes you to the beginning of the line

b. 'CTRL-e' - takes you to the end of the line

c. 'CTRL-b' - back one character

d. 'CTRL-f' - forward one character

You might also like