Combining Multiple Commands
mkdir new_dir && cd new_dir
(Create a directory and move into it only if successful)
touch file1.txt; touch file2.txt
(Create two files, one after the other)
command1 || echo 'command1 failed'
(Execute command2 only if command1 fails)
cp file.txt backup/ && rm file.txt
(Copy file to backup and remove the original)
Environment Variables
export PATH=$PATH:/usr/local/bin
(Add directory to PATH)
export EDITOR=nano
(Set default text editor to nano)
export HISTSIZE=1000
(Set command history size)
echo $HOME
(Print the home directory path)
Running Commands in Background
or Virtual Consoles
long_running_command &
(Run a command in the background)
sleep 60 &
(Sleep for 60 seconds in the background)
fg %1
(Bring the first background job to the foreground)
Ctrl+Alt+F1 to F6
(Switch between virtual consoles)
Use of Aliases
alias ll='ls -l'
(Create alias ll for ls -l)
alias rm='rm -i'
(Create alias to ask for confirmation before deletion)
unalias ll
(Remove the alias ll)
alias home='cd ~'
(Create alias to change directory to home)
Use of Pipelining
cat file.txt | grep 'search_term'
(Search for term in file content)
ps aux | grep 'httpd'
(Filter running processes for 'httpd')
df -h | grep '/dev/sda1'
(Filter disk space output for a specific partition)
find . -type f | wc -l
(Count the number of files in current directory)
Clobbering
echo 'New content' > file.txt
(Overwrite file.txt with new content)
ls > file_list.txt
(Overwrite file_list.txt with the output of ls)
echo 'data' >> file.txt
(Append data to file.txt)
command >| file.txt
(Force overwriting file.txt, even with noclobber set)