My CLI cheatsheet
This page is a little cheat sheet for commands that I use often, but don't always remember well enough to get right on the first try.
Changing file permissions
- make all files in a directory read/write:
chmod -R 644 * - make a file executable:
chmod +x <file_name>
Finding stuff in files
- get a list of all files in directory that contain a string:
grep -l '<string>' *.* - search all files in a directory for a string and output the file name and line the string appears in to a file in the parent directory *:
grep -Eir '<string>' . >> ../<output_file> - get all lines in file that don't contain a string and output to a file in the parent directory *:
cat <input_file> | grep -v '<string>' >> ../<output_file> - show the differences between two files with side by side output:
diff -y <file_1> <file_2>
Copying files between machines
- scp a file from my local machine to a server:
scp <file_name> <user>@<ip_address>: - scp a file from a server to my local machine:
scp <user>@<ip_address>:<file_name> <file_name> - download a file with curl:
curl <url> -o <output_file>
Manipulating text in files
- search and replace text in all files in directory:
sed -i.bak 's/<text_to_replace>/<new_text>/g' * - search and replace text in all files of one type in a directory recursively:
find . -type f -name "*.<ext>" -exec sed -i.bak 's/<text_to_replace>/<new_text>/g' {} + - delete the backup files after running one of those sed commands:
find . -name '*.bak' -delete - Prepend and append text to all lines in a file:
awk '{ print "<prepend_text>", $0, "<append_text>" }' <input_file> > <output_file>
Listing files
- find all files with given extension recursively:
find . -type f -name "*.<ext>" - list all files in a directory, including hidden files:
ls -a - list all files in a directory in one column:
ls -1 - list all files in a directory with a specific extension:
ls *.<ext>orls *.{<ext_1>,<ext_2>}
Working with zip archives
- see what's in a zip archive:
unzip -vl <zip_file> - add a file to a zip archive:
zip -u <zip_file> <file_name> - zip a directory:
zip -r <zip_file> <folder>
Miscellaneous
- Adding a folder to PATH:
export PATH=<full_path_to_folder>:$PATH