Linux Bash Cheat Sheet
5 minute read
Linux Bash Commands Cheat Sheet
Bash is the default shell on most Linux systems.
Basic Commands
Here are some basic bash commands:
Navigation
cd <directory>
: Change the current working directory todirectory
cd ..
: Move up one directory levelpwd
: Print the current working directory
File and directory management
ls
: List the files and directories in the current directoryls <directory>
: List the files and directories indirectory
ls -l
: List the files and directories in the current directory in long formatls -a
: List all files and directories, including hidden onesmkdir <directory>
: Create a new directorydirectory
touch <file>
: Create a new filefile
cp <source> <destination>
: Copysource
todestination
mv <source> <destination>
: Movesource
todestination
rm <file>
: Deletefile
rmdir <directory>
: Deletedirectory
(if it is empty)
Text manipulation
cat <file>
: Print the contents offile
to the terminalhead <file>
: Print the first 10 lines offile
to the terminaltail <file>
: Print the last 10 lines offile
to the terminalsort <file>
: Sort the lines offile
grep <pattern> <file>
: Print the lines infile
that containpattern
Process management
ps
: List the running processestop
: Display the top running processeskill <pid>
: Terminate the process with the process IDpid
killall <process>
: Terminate all processes with the nameprocess
System information
uname
: Print the current system informationuname -a
: Print all system informationuptime
: Print the current uptimefree
: Print the memory usagedf
: Print the disk usagedu
: Print the directory space usage
Networking
ping <host>
: Send a ping request tohost
traceroute <host>
: Print the route tohost
host <hostname>
: Look up the IP address ofhostname
nslookup <hostname>
: Look up the DNS information forhostname
ifconfig
: Print the network interface information
Users and groups
whoami
: Print the current userwho
: Print the users currently logged inadduser <user>
: Add a new useruser
addgroup <group>
: Add a new groupgroup
usermod -a -G <group> <user>
: Adduser
togroup
passwd <user>
: Change the password foruser
Advanced Commands
Here are some advanced bash commands:
Shell scripts
#! /bin/bash
: Shebang indicating the path to the bash interpreter$0
: The name of the script$1
-$9
: The first 9 arguments passed to the script$@
: All arguments passed to the script$#
: The number of arguments passed to the script$?
: The exit status of the last command$!
: The process ID of the last background command
Loops
for i in <list>; do <commands>; done
: Iterate throughlist
, executingcommands
each timewhile <condition>; do <commands>; done
: Executecommands
whilecondition
is trueuntil <condition>; do <commands>; done
: Executecommands
untilcondition
is trueConditionals
if <condition>; then <commands>; fi
: Executecommands
ifcondition
is trueif <condition>; then <commands>; else <other commands>; fi
: Executecommands
ifcondition
is true, otherwise executeother commands
Functions
function <name> { <commands>; }
: Define a functionname
with the commandscommands
<name> <arguments>
: Call the functionname
with the argumentsarguments
Variables
<variable>=<value>
: Set the value ofvariable
tovalue
$<variable>
: Access the value ofvariable
<variable>=$(<command>)
: Set the value ofvariable
to the output ofcommand
Redirection
<command> > <file>
: Redirect the output ofcommand
tofile
(overwrite)<command> >> <file>
: Redirect the output ofcommand
tofile
(append)<command> < <file>
: Redirect the input ofcommand
fromfile
<command> 2> <file>
: Redirect the error output ofcommand
tofile
<command> 2>> <file>
: Redirect the error output ofcommand
tofile
(append)
Pipes
<command1> | <command2>
: Pipe the output ofcommand1
as the input ofcommand2
Background processes
<command> &
: Runcommand
in the background
Aliases
alias <name>='<command>'
: Set an aliasname
for the commandcommand
alias
: List all aliases
Environment variables
export <variable>=<value>
: Set an environment variablevariable
tovalue
echo $<variable>
: Print the value of the environment variablevariable
Shell options
set -o <option>
: Enable the shell optionoption
set +o <option>
: Disable the shell optionoption
History
history
: List the command history!<number>
: Execute the command with the history numbernumber
!!
: Execute the last command!<string>
: Execute the most recent command starting withstring
I feedback.
Let me know what you think of this article on twitter @cpardue09 or leave a comment below!
Let me know what you think of this article on twitter @cpardue09 or leave a comment below!
comments powered by Disqus