Last Updated: Thursday, 14 December 2017 14:20
Below are a number of commonly used commands in unix. For almost every command, there are additional flags that can be used to modify the command's behavior. For example, typing 'ls' in the terminal displays the files contained within the current working directory, but 'ls -l' displays them in a list format. Additionally, 'ls -lt' displays them in a list, but also in order of modification time. Information on what flags can be used, along with general explanations about the commands, can be found by typing 'man' before the command. Ex: 'man ls'.
Basic navigation is performed using a few commands:
cd: Short for 'change directory', changes the current working directory.
ls: Short for 'list', lists all of the files in the current working directory.
cwd: Short for 'current working directory', lists the absolute path to the directory the user is in.
clear: Clears the terminal of all text, although the previous text can still be seen by scrolling up.
In navigating a Unix file structure, a number of shorthand notations should be mentioned. Example usage of the above commands are shown here as well:
'~': The location of a user's home directory. When logging into a unix machine, you are likely going to begin in your home directory, where your personal files will be located. Typing 'ls' while in the home directory will display folders like 'Desktop', Pictures', 'Downloads', and 'Documents'. To move into one of these folders, one can type, e.g., 'cd Downloads'. Additionally, to display the contents of the 'Downloads' folder without navigating to it, type 'ls Downloads'.
'/': The machine's root directory. This is the highest level directory that contains all other files and folders in the machine. Programming libraries, system files, etc are found within the folders here. Typing 'cwd' in any directory will display the path from the root directory to the current working directory. For example, if the user's name was 'tom', typing 'cwd' while in the 'Downloads' folder will display '/home/tom/Downloads'.
'.': The current directory is represented with a simple period. Typing 'cd .' moves you to the current working directory, i.e. to where you are now.
'..': The directory above the current working directory. Ex: If you're in the directory ~/Downloads/, typing 'cd ..' will bring you to the home directory.
'-': This represents the last directory you were in. Ex: If you were in '~/Downloads' and moved to '/lib64/', typing 'cd -' would move you back to '~/Downloads' and 'ls -' would display the files within '~/Downloads'.
Creating, deleting, moving, renaming files/folders:
mkdir: Short for 'make directory', creates a directory with a given name. Ex: 'mkdir ~/Desktop/DSC' creates a folder 'DSC' in the 'Desktop' folder in your home directory.
rmdir: Short for 'remove directory', deletes a directory. Note that there are no backups, so be careful when using this command.
touch: Generally used to create a file with a given name. Ex: 'touch ~/Downloads/example.txt' creates an empty file named 'example.txt' in the Downloads folder and closes it. This command was originally used to update the time a file was accessed/modified -- 'touching' it by opening it if it exists, not modifying anything, and closing it.
cp: Short for 'copy', simply copies a file. Ex: If a file named 'example.txt' were in the Downloads folder and the Downloads folder is the current working directory, we could copy it into to the Desktop by using 'cp example.txt ~/Desktop/'. We could also rename the copy something else by using 'cp example.txt ~/Desktop/example2.txt'. To copy a folder, the '-rf' flag is required, as it needs to recursively copy all the files within the folder if they are present.
mv: Short for 'move', moves a file from one place to another, and is often used to rename a file. This command effectively does the same thing as 'cp', only that the original file is removed.
rm: Short for 'remove', deletes a file. Note that there are no backups of these files, so be careful when using this command. When deleting a directory, use 'rm -rf' or 'rmdir'.
locate: Searches the entirety of the file structure for a file/directory with a given name.
Tab-completion: When typing in commands, especially when trying to navigate file structures, using the tab key can auto-complete the names of files/folders if they are present. If nothing auto-completes after typing tab once, type it again to see all possible options. Similar to our example above under the 'cp' explanation, if we were to type 'ls ~/Desktop/example' and hit tab twice, it would show 'example.txt' and 'example2.txt'. Typing in '.' would then let it auto-complete the word 'example.txt' after hitting tab again.
Arrows: Use the up arrow to access previous commands that have been typed in. Using the down arrow after using the up arrow will go back down the list of commands.
Permissions: A fundamental part of the unix file system is permissions (which cannot be fully explained here) and is generally accessed by using 'ls -l', i.e. displaying a folder's contents in list format. If you cannot read or modify a file, check the permissions. The permissions are on the left and dictate who can and cannot read (r), write (w), or execute (x) files in Unix (called access modes). Ex: If one has read access, the 'r' will be displayed. If one does not have read access, the 'r' column will be replaced with a dash '-'. Further, these are split into three sets. The first set of access modes are the permissions of the file's owner (the first name beside the permissions). The second set are the permissions of the file's group (the second name beside the permissions). The third set is the permissions for everyone else. Ex: If a file has only read access, the permissions would read '-r--r--r--'. This means that the user, the user's group, and anyone else can open and read the contents of the file. If it had read, write, and execute access for only the user, it would read '-rwxr--r--'. Here the user can open the file to read its contents, save edits to it, and execute it (if it's a program or a script), but everyone else can only read its contents. If it further had execute access for the user's group, it would read '-rwxr-xr--'. Finally, notice that all of these examples have a leading '-'. This is replaced with a 'd' only if it is a directory. In that case, the number beside the permissions would display how many files are in the folder.
A final example for a folder named 'DSC' with 3 files in it owned by a user named 'tom' in the group 'users' with read, write, and execute permissions for the user 'tom', read and execute permissions for the group 'users', and read permissions for everyone else, with a byte size of 1000 and last modified on August 1st, 2017, would be:
drwxr-xr-- 3 tom users 1000 Aug 1 2017 DSC