Skip to content

Instantly share code, notes, and snippets.

@alexandre-delaloy
Last active March 16, 2023 12:25
Show Gist options
  • Save alexandre-delaloy/b05a8bf28eaef095c47d7d9c788fce61 to your computer and use it in GitHub Desktop.
Save alexandre-delaloy/b05a8bf28eaef095c47d7d9c788fce61 to your computer and use it in GitHub Desktop.
Bash Command Line Cheatsheet
------------------------------------------------------------------------------------------------
| BASH COMMAND LINE CHEATSHEET |
| |
| by Alexandre Delaloy - @blyndusk |
------------------------------------------------------------------------------------------------
Notation
[D] Directory
[D...] Directory, directories
[F] File
[F...] Files
[FD] File, directory
[FD...] File, files, directory, directories
---------------------------------------------------------------------------------------------------
Getting help:
----
man [command] Read the manual for a given command
[command] -h | --help Ask the command for help
e.g.
man chmod
chmod -h
---------------------------------------------------------------------------------------------------
Listing directory content
----
ls [FD...] List file(s) or director(y/ies) at [FD...]
-- --
ls [FD1...] [FD2...] List both [FD1...] and [FD2...]
ls -l [FD...] Long listing format
ls -a [FD...] Show all files, including hidden files
ls -R [FD...] Recursive listing
ls -F [FD...] Show type of each file
(/=directory, *=executable, @=link)
e.g.
ls ~/foo
ls -la $HOME ./bar
---------------------------------------------------------------------------------------------------
Changing directories
----
cd [D] Change into the directory at [D]
cd Change into your home directory ($HOME)
cd .. Go to the parent directory
cd ../../ n times Go up n parent directories
(cd ../../../ => 3 parents directories)
cd - Change to the previous directory
pwd Return working directory fullname
e.g.
cd ~/foo
cd ../../../../
---------------------------------------------------------------------------------------------------
Creating files and directories
----
mkdir [D...] Create director(y/ies) at [D...]
-- --
mkdir -p [D...] Create one or more parents directories if needed
(mkdir -p foo/bar => 2 parents directories)
touch [F...] Create file(s) if doesn't exist
e.g.
mkdir ./bar
mkdir -p ~/foo/bar ./bar
touch ~/foo/bar/foobar.txt
touch foo.txt ~/foo/bar.txt
---------------------------------------------------------------------------------------------------
Removing files and directories
----
rmdir [D...] Remove director(y/ies) at [D...] (it must be empty)
rm [F...] Remove file(s) at [F...]
-- --
rm -r [FD...] Recursively remove all files, subdirectories and
files into subdirectories starting at [FD...]
rm -i [F...] Run in interactive mode
rm -f [F...] Remove regardless of the [F...]'s permissions
e.g.
rmdir ~/foo ./bar
rm -f ~/foo/bar/foobar.txt
rm -ri ~/foo/bar
rm -rf ~/foo ./bar
---------------------------------------------------------------------------------------------------
Copying, moving and renaming files and directories
----
[cp/mv] [F...] [D] Copy/Move file(s) to [D]
-- --
cp -r [FD...] [D] Copy file(s) and director(y/ies) recursively to [D]
(directory, files, subdir and subfiles)
mv [OLDFILE] [NEWFILE] Rename [OLDFILE] to [NEWFILE]
e.g.
[cp/mv] foo.txt bar.txt ~/bar/
cp -r ~/foo/ ~/bar/
mv foo.txt bar.txt
---------------------------------------------------------------------------------------------------
Viewing files
----
cat [F...] Display the content of file(s)
less [F...] Browse throught a text file(s)
head [F...] Output the top portion of file(s)
tail [F...] Output the bottom of file(s)
--
cat -n [F...] Display with line numbers output
[head/tail] -n [F...] Display n number of lines (10 by default)
e.g.
cat -n foo.txt
head -6 foo.txt
---------------------------------------------------------------------------------------------------
Editing files
----
vim [F...] Use the vim editor to view and modify a file(s)
nano [F...] Use the nano editor to view and modify a file(s)
e.g.
vim foo.txt
nano bar.txt
---------------------------------------------------------------------------------------------------
Sorting files content
----
sort [F...] Sort or merge lines of text and binary file(s)
-- --
sort -r [F...] Sort in reverse order
sort -u [F...] Suppress all lines that is equal to another
sort -o [OUTPUT_FILE] [F...] Print the output to [OUTPUT_FILE]
sort -k n [F...] Sort by key (n is the field number)
e.g.
sort -ru -o bar.txt foo.txt
sort -k 2 foo.txt
---------------------------------------------------------------------------------------------------
Manipulating tar archive(s)
----
tar -[ctx]f [OUTPUT_FILE] [FD...] Manipulate archive from file(s) and director(y/ies)
Options
-c Create a tar archive
-x Extract files from the archive
-t Display the table of contents (list)
-v Be verbose
-z Use compression
-f [OUTPUT_FILE] Output archive file to [OUTPUT_FILE]
e.g.
tar -czvf foo.gz ~/foo Create a archive named 'foo.gz' from the 'foo' dir
using verbose and compression
tar -tf foo.gz List 'foo.gz' files in archive
tar -xzf foo.gz Extract 'foo.gz' content in the workdir
---------------------------------------------------------------------------------------------------
Displaying disk usage stats
----
du Estimate file(s) usage (all files in workdir)
du [FD...] Estimate file(s) usage at [FD...]
du -h Display size in human readable format (octets)
du -k Display size in Kilobytes
---------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment