Bash Cheat Sheet
A cheat sheet for bash commands.
Command History
!! # Run the last command
touch foo.sh
chmod +x !$ # !$ is the last argument of the last command i.e. foo.sh
Navigating Directories
pwd # Print current directory path
ls # List directories
ls -a|--all # List directories including hidden
ls -l # List directories in long form
ls -l -h|--human-readable # List directories in long form with human readable sizes
ls -t # List directories by modification time, newest first
stat foo.txt # List size, created and modified timestamps for a file
stat foo # List size, created and modified timestamps for a directory
tree # List directory and file tree
tree -a # List directory and file tree including hidden
tree -d # List directory tree
cd foo # Go to foo sub-directory
cd # Go to home directory
cd ~ # Go to home directory
cd - # Go to last directory
pushd foo # Go to foo sub-directory and add previous directory to stack
popd # Go back to directory in stack saved by `pushd`
Creating Directories
mkdir foo # Create a directory
mkdir foo bar # Create multiple directories
mkdir -p|--parents foo/bar # Create nested directory
mkdir -p|--parents {foo,bar}/baz # Create multiple nested directories
mktemp -d|--directory # Create a temporary directory
Moving Directories
cp -R|--recursive foo bar # Copy directory
mv foo bar # Move directory
rsync -z|--compress -v|--verbose /foo /bar # Copy directory, overwrites destination
rsync -a|--archive -z|--compress -v|--verbose /foo /bar # Copy directory, without overwriting destination
rsync -avz /foo username@hostname:/bar # Copy local directory to remote directory
rsync -avz username@hostname:/foo /bar # Copy remote directory to local directory
Deleting Directories
rmdir foo # Delete empty directory
rm -r|--recursive foo # Delete directory including contents
rm -r|--recursive -f|--force foo # Delete directory including contents, ignore nonexistent files and never prompt
Creating Files
touch foo.txt # Create file or update existing files modified timestamp
touch foo.txt bar.txt # Create multiple files
touch {foo,bar}.txt # Create multiple files
touch test{1..3} # Create test1, test2 and test3 files
touch test{a..c} # Create testa, testb and testc files
mktemp # Create a temporary file
echo "foo" > bar.txt # Overwrite file with content
echo "foo" >> bar.txt # Append to file with content
ls exists 1> stdout.txt # Redirect the standard output to a file
ls noexist 2> stderror.txt # Redirect the standard error output to a file
ls 2>&1 > out.txt # Redirect standard output and error to a file
ls > /dev/null # Discard standard output and error
read foo # Read from standard input and write to the variable foo
Moving Files
cp foo.txt bar.txt # Copy file
mv foo.txt bar.txt # Move file
rsync -z|--compress -v|--verbose /foo.txt /bar # Copy file quickly if not changed
rsync z|--compress -v|--verbose /foo.txt /bar.txt # Copy and rename file quickly if not changed
Deleting Files
rm foo.txt # Delete file
rm -f|--force foo.txt # Delete file, ignore nonexistent files and never prompt
Reading Files
cat foo.txt # Print all contents
less foo.txt # Print some contents at a time (g - go to top of file, SHIFT+g, go to bottom of file, /foo to search for 'foo')
head foo.txt # Print top 10 lines of file
tail foo.txt # Print bottom 10 lines of file
open foo.txt # Open file in the default editor
wc foo.txt # List number of lines words and characters in the file
File Permissions
# |
Permission |
rwx |
Binary |
7 |
read, write and execute |
rwx |
111 |
6 |
read and write |
rw- |
110 |
5 |
read and execute |
r-x |
101 |
4 |
read only |
r– |
100 |
3 |
write and execute |
-wx |
011 |
2 |
write only |
-w- |
010 |
1 |
execute only |
–x |
001 |
0 |
none |
— |
000 |
For a directory, execute means you can enter a directory.