0% found this document useful (0 votes)
5 views

Linux Command Examples Updated

jk

Uploaded by

khadija.bari.des
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Linux Command Examples Updated

jk

Uploaded by

khadija.bari.des
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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)
Combining Multiple Commands
echo 'Start' && mkdir project && cd project && echo 'Done'
(Create a project directory, move into it, and display 'Done')

command1; command2; command3


(Execute commands in sequence regardless of success or failure)

ping -c 3 google.com || echo 'Ping failed'


(Ping Google and display error message if it fails)

rm -rf /tmp/* && echo 'Temp files cleaned'


(Remove all files in /tmp and display a message)
Environment Variables
export PS1='\u@\h:\w\$ '
(Customize the shell prompt to show username, hostname, and working directory)

export LANG=en_US.UTF-8
(Set language and locale settings)

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
(Set the JAVA_HOME variable)

printenv | grep PATH


(View the current PATH environment variable)
Running Commands in Background
or Virtual Consoles
top &
(Run top in the background)

nohup command &


(Run a command in the background and ignore hangup signals)

jobs
(List all background jobs)

bg %1
(Resume a suspended job in the background)
Use of Aliases
alias gs='git status'
(Create alias gs for git status command)

alias update='sudo apt-get update && sudo apt-get upgrade'


(Create alias for updating system packages)

alias cls='clear'
(Create alias cls for clear command)

alias grep='grep --color=auto'


(Create alias to highlight grep matches with color)
Use of Pipelining
cat /var/log/syslog | grep 'error' | less
(Filter syslog for errors and view with less)

find . -name '*.log' | xargs rm


(Delete all log files found in the current directory and subdirectories)

history | tail -n 10 | awk '{print $2}'


(Display the last 10 commands entered)

du -h | sort -hr | head -n 10


(Show the top 10 largest directories and files)
Clobbering
echo 'Reset file' > reset.txt
(Overwrite reset.txt with the content 'Reset file')

cat /dev/null > largefile.txt


(Empty the contents of largefile.txt)

echo 'This will replace everything' > important.txt


(Replace all content in important.txt)

cp source.txt destination.txt >| log.txt


(Force copy and log the output, even if noclobber is set)

You might also like