Commonly Used Linux / SSH Commands
File Management
ls | Lists the contents of your working directory. ls -a Lists all of your files, including the “dot” files. There are a number of files that start with a . that won’t normally show up when you type ls. An example of such a file is your .newsrc file ls -s Gives you a listing of your files along with their size in kilobytes. ls -l Gives you a “long listing,” which means that your files will be shown in one vertical column and the file size and file access modes for all files will be shown. All of these switches can be combined, so one might type ls -as to list all of one’s files and find their size. |
cp | Copies a file to a specified location. Can be used to copy a directory with files in it using: cp –r Usage: cp[ -r ][source file(s) or directory(ies)] [destination file(s) or directory(ies)] Examples: cp ~/myfile ~/mydir/ cp file1.txt newfile.txt cp –r ~/myproject/ ~/myprojectbackup/ |
mv | Moves (or renames) a file to a specified location. Similar to cp except that it deletes the source file (or directory, no option needed). Similar to cut and paste. Usage: mv [source file(s) or directory(ies)] [destination file or directory(ies)] Examples: mv ~/myoldfilename ~/mynewfilename mv file* ~/mydirectoryoffiles/ mv ~/mydir/ ~/myotherdir/ |
rm | Deletes file(s) or directory trees. There is no undoing! Can be used to delete all files and subdirectories with rm –rf . Usage: rm [-rf] [file or directory] Examples: rm mytxtfile.txt rm –rf ~/mydir/ (removes the directory and everything in it) rm –rf ~/ (don’t do this!) |
/bin/rm -rf | It’s just like above, except you won’t get the very data saving confirmation messages if you’re deleting many files. An alternative is to unalias rm which will use /bin/rm until you logout. |
pwd | Prints the working directory. This could be called your current path or view. |
cd | Changes your working directory or view or where you’re “in” to another specified location. Usage: cd [destination path] Examples: cd ~/ (home directory) cd somedir/someotherdir/ cd / (root directory) cd .. (parent directory) cd – (previous directory) |
mkdir | Creates a new directory. You can specify a location if you like. Usage: mkdir [directory path and name] Examples: mkdir newdir mkdir ~/newdir mkdir olddir/newdir mkdir -p newdir/newdir2 (makes parent directories as needed) |
rmdir | Removes (deletes) a directory. You can only use this on empty directories. Usage: rmdir [directory path and name] Examples: rmdir ~/olddir rmdir olddir/empty/ |
du -k | Lists all files and folders with their associated total sizes in KiloBytes. Usage: du –k [directory path or file] Examples: du –k (from current directory) du –ks ~/.mozilla (displays sum of all files’ sizes in your mozilla tree) |
uquota -w | Displays your current disk and print quota usage. The –w option allows you to see work directory quotas as well. Usage: uquota –w |
chmod | Modifies the permissions of files or directories. Permissions are rules about how and which users can access the files or directories. You can use the symbols or numeric mode to make the changes. We will cover the symbolic mode, the three switches covered here are: r -read access, w -write access, x –execute access. Usage: chmod [u,g,o][+,-][r,w,x] [file(s) or directory] Examples: chmod +r myfile (adds read access for everyone for myfile) chmod a+r myfile (same as above) chmod g+r myfile (adds read access to group only) chmod –w myfile (removes write access to myfile) chmod +x myfile (adds execute access to myfile) chmod ug=rw,o=r myfile (sets user and group to read and write, others to read) |
ln -s | Creates a symbolic link to a file or directory. Symbolic links are analagous to windows shortcuts, and are simply pointers. You can create one in your home directory to avoid having to cd into another directory you use frequently. Usage: ln –s [link file] [target] Examples: ln -s fortunelink /usr/local/share/terminfo/f/fortune ln –s otherlink ~/something/somethingelse |
Copy command ‘cp’ – and Delete ‘rm’ Quick Reference
example: cp -Rvnp folderA folderB
where the ‘-‘ flags are:
- R = recursive, go through each folder
- v = verbose, output stuff
- n = no clobber. Don’t overwrite files
- p = preserve file attributes
- a = archive files
- f = force copy by removing the destination file if needed
- i = interactive – ask before overwrite
- l = link files instead of copy
- L = follow symbolic links
- n = no file overwrite
- R = recursive copy (including hidden files)
- u = update – copy when source is newer than dest
- v = verbose – print informative messages
remove all contents of a directory including sub: rm -rf <directory>
remove an empty directory: rmdir <directory>
Unzip / Untar tar.gz or tgz files
Most Linux distributions and macOS comes with tar command pre-installed by default.
To extract a tar.gz file, use the –extract (-x) option and specify the archive file name after the f option:
tar -xf archive.tar.gz
The tar command will auto-detect compression type and will extract the archive. The same command can be used to extract tar archives compressed with other algorithms such as .tar.bz2 .
If you are a Desktop user and the command-line is not your thing you can use your File manager. To extract (unzip) a tar.gz file simply right-click on the file you want to extract and select “Extract”. Windows users will need a tool named 7zip to extract tar.gz files.
The -v option will make the tar command more visible and print the names of the files being extracted on the terminal.
tar -xvf archive.tar.gz
By default, tar will extract the archive contents in the current working directory . Use the –directory (-C) to extract archive files in a specific directory:
For example, to extract the archive contents to the /home/linuxize/files directory, you can use:
tar -xf archive.tar.gz -C /home/linuxize/files
Extracting Specific Files from a tar.gz File
To extract a specific file(s) from a tar.gz file, append a space-separated list of file names to be extracted after the archive name:
tar -xf archive.tar.gz file1 file2
When extracting files, you must provide their exact names including the path, as printed by –list (-t).
Extracting one or more directories from an archive is the same as extracting files:
tar -xf archive.tar.gz dir1 dir2
You can also extract files from a tar.gz file based on a wildcard pattern, by using the –wildcards option and quoting the pattern to prevent the shell from interpreting it.
For example, to extract files whose names end in .js (Javascript files), you would use:
tar -xf archive.tar.gz –wildcards ‘*.js‘
From https://linuxize.com/post/how-to-extract-unzip-tar-gz-file/
Working in Bash / SSH
- Ctrl+C: Interrupt (kill) the current foreground process running in in the terminal. This sends the SIGINT signal to the process, which is technically just a request—most processes will honor it, but some may ignore it.
- Ctrl+Z: Suspend the current foreground process running in bash. This sends the SIGTSTP signal to the process. To return the process to the foreground later, use the fg process_name command.
- Ctrl+D: Close the bash shell. This sends an EOF (End-of-file) marker to bash, and bash exits when it receives this marker. This is similar to running the exit command.
Controlling the Screen
The following shortcuts allow you to control what appears on the screen.
- Ctrl+L: Clear the screen. This is similar to running the “clear” command.
- Ctrl+S: Stop all output to the screen. This is particularly useful when running commands with a lot of long, verbose output, but you don’t want to stop the command itself with Ctrl+C.
- Ctrl+Q: Resume output to the screen after stopping it with Ctrl+S.
Moving the Cursor
Use the following shortcuts to quickly move the cursor around the current line while typing a command.
- Ctrl+A or Home: Go to the beginning of the line.
- Ctrl+E or End: Go to the end of the line.
- Alt+B: Go left (back) one word.
- Ctrl+B: Go left (back) one character.
- Alt+F: Go right (forward) one word.
- Ctrl+F: Go right (forward) one character.
- Ctrl+XX: Move between the beginning of the line and the current position of the cursor. This allows you to press Ctrl+XX to return to the start of the line, change something, and then press Ctrl+XX to go back to your original cursor position. To use this shortcut, hold the Ctrl key and tap the X key twice.
Deleting Text
Use the following shortcuts to quickly delete characters:
- Ctrl+D or Delete: Delete the character under the cursor.
- Alt+D: Delete all characters after the cursor on the current line.
- Ctrl+H or Backspace: Delete the character before the cursor.
Fixing Typos
These shortcuts allow you to fix typos and undo your key presses.
- Alt+T: Swap the current word with the previous word.
- Ctrl+T: Swap the last two characters before the cursor with each other. You can use this to quickly fix typos when you type two characters in the wrong order.
- Ctrl+_: Undo your last key press. You can repeat this to undo multiple times.
Cutting and Pasting
Bash includes some basic cut-and-paste features.
- Ctrl+W: Cut the word before the cursor, adding it to the clipboard.
- Ctrl+K: Cut the part of the line after the cursor, adding it to the clipboard.
- Ctrl+U: Cut the part of the line before the cursor, adding it to the clipboard.
- Ctrl+Y: Paste the last thing you cut from the clipboard. The y here stands for “yank”.
Capitalizing Characters
The bash shell can quickly convert characters to upper or lower case:
- Alt+U: Capitalize every character from the cursor to the end of the current word, converting the characters to upper case.
- Alt+L: Uncapitalize every character from the cursor to the end of the current word, converting the characters to lower case.
- Alt+C: Capitalize the character under the cursor. Your cursor will move to the end of the current word.
Tab Completion
RELATED: Use Tab Completion to Type Commands Faster on Any Operating System
Tab completion is a very useful bash feature. While typing a file, directory, or command name, press Tab and bash will automatically complete what you’re typing, if possible. If not, bash will show you various possible matches and you can continue typing and pressing Tab to finish typing.
- Tab: Automatically complete the file, directory, or command you’re typing.
For example, if you have a file named really_long_file_name in /home/chris/ and it’s the only file name starting with “r” in that directory, you can type /home/chris/r, press Tab, and bash will automatically fill in /home/chris/really_long_file_name for you. If you have multiple files or directories starting with “r”, bash will inform you of your possibilities. You can start typing one of them and press “Tab” to continue.
Working With Your Command History
RELATED: How to Use Your Bash History in the Linux or macOS Terminal
You can quickly scroll through your recent commands, which are stored in your user account’s bash history file:
- Ctrl+P or Up Arrow: Go to the previous command in the command history. Press the shortcut multiple times to walk back through the history.
- Ctrl+N or Down Arrow: Go to the next command in the command history. Press the shortcut multiple times to walk forward through the history.
- Alt+R: Revert any changes to a command you’ve pulled from your history if you’ve edited it.
Bash also has a special “recall” mode you can use to search for commands you’ve previously run:
- Ctrl+R: Recall the last command matching the characters you provide. Press this shortcut and start typing to search your bash history for a command.
- Ctrl+O: Run a command you found with Ctrl+R.
- Ctrl+G: Leave history searching mode without running a command.