The practical command line cheat sheet

From BITS wiki
Jump to: navigation, search

Listing contents

ls -dlh
List the files and directories with their permissions and size
  • d = 'directory', list the directory instead of the contents
  • l = 'long listing', provide a lot of details
  • h = 'human readable', print the size in KB, MB, GB,...
ls -dalh
Include hidden files and folders
  • a = 'all', include hidden files and folders
  • d = 'directory', list the directory instead of the contents
  • l = 'long listing', provide a lot of details
  • h = 'human readable', print the size in KB, MB, GB,...
ls *<word>*
List files and directories containing the word.
  • '*' is a wildcard, replacing any string of any length.

Managing files

mv <filename>{,.back}
Rename file with <filename> to <filename.back>
  • {...} is brace expansion. It replaces the complete structure by repeating in a particular way the content in it. Here the result is 'mv <filename> <filename>.back'.
rm -rf <files>
Forcefully remove file.
  • r = recursively. Also remove all subdirectories and their contents
  • f = force. Do not ask for confirmation.
rm -rf *<string>*
Forcefully remove files which contain <string> in their name
chown -R username:groupname <directory_name/>
change directory and all its contents to be owned by user username and group groupname.
  • R = recursively. Also act on all subdirectories and their contents
chmod u+x <file>
Set the execution permissions to file filename
  • u = user. Apply for the user that owns the file only. Other options: g (group) and o (others)
  • + = add permission. Other option: - (remove permission)
  • x = execute permission. Other options: r (read), w (write).

Searching content

grep <string> <file>
Display lines in file <file> containing the string <string>
grep <string> *
Search in all files in current directory
grep -R <string> *
Search in all files in current directory and all subdirectories
  • R = recursively. Also remove all subdirectories and their contents
grep -Rl <string> *
List the file names in which the string appears
  • R = recursively. Also remove all subdirectories and their contents
  • l = list. Only show file names.
find /usr/local/bin -type l
Find all symbolic links in the directory /usr/local/bin
  • -type l = link. Other options: f (file), d (directory)
find -type d -name bin
find directories with name bin in current directory.
find -name \*.gtf
find files or directories ending with .gtf.
  • the * needs to be escaped for find to work.
find . -type f -perm +111 -exec ln -s `pwd`/{} /usr/local/bin \;
Find in current directory all files with permissions set, and put a symbolic link to them in the folder /usr/local/bin
  • perm = permissions, according to the numerical permission settings.
  • exec = execute following command. Replace '{}' with the found item. End always with '\;'

Storage

ls -lhd *
List the sizes of the files in current directory
du -sh *
List the size of files in current directory
du -sh */
List the sizes of the directories in current directory
df -h
List the space left on the storage
df -h .
List the space left on the currently used disk
mount
Show which disks are available on the system
showmount -e <host>
List the NFS network shares offered by host <host> (IP address or hostname)

Terminal usage

ctrl+shift+T Fire up the terminal (can be customized of course)
ctrl+U clear line
ctrl+D close terminal (log out)
ctrl+a move to beginning of line
ctrl+e move to end of line
alt+f move forward one word
alt+b move backwards one word
ctrl+l clear sceen, leaving current line
pushd <path_to_dir> Store the path to the directory <path_to_dir>, and cd to it
popd Enter the last stored path with pushd.
sudo su - Become superuser.

Linking

ln -s <what> <towhere>
Preferentially move first to the folder where to create the symbolic link
unlink
Remove a link