Command Line Basics
This page serves as an introduction to basic commands for managing your file system on Linux. Much of what is reviewed on this page is introduced as part of the computing course in Immigration to Statistics.
These commands can be used on any operating system using Linux. To get started, navigate to your terminal, or log in to any of the department servers.
Creating a Directory
To create a new directory, you can use the mkdir command, which quite literally stands for "make directory." Simply type the command followed by the name of the directory you wish to create:
mkdir MyDirectoryMoving Between Directories
The change directory command is called cd and can be used to efficiently switch between directories. To move into our newly created directory, type:
cd MyDirectoryIf we want to go backwards, we can use .., which always means to go back one folder from where you currently are.
cd ..If we want to return to our root directory, we can simply type the command with nothing after it.
cdI can check my location using the present working directory command or pwd. Typing pwd into the terminal will return the path to your current working directory (i.e. where you are currently located in your file system) from the root.
Moving and Copying Files
Say I have a file in MyDirectory called myfile. I might want to move myfile into MySubDirectory. To do this, I would do as follows:
mv myfile MySubDirectory/The first thing after mv represents the file to move, and the second represents the destination. mv is also good for renaming files. I can change the name of myfile to mynewname as follows:
mv myfile mynewnameThis just renames the file in place without moving it elsewhere.
Sometimes, we don't want to move files but we do want to copy them. This is the cp command, and it works much the same way as the mv command. To use cp, you must specify the file or folder you wish to copy, and the destination to where you want to copy it.
cp myfile ../This creates a new copy of myfile in the previous directory on the path to my current working directory. If I have a directory, I might want to copy its entire contents to a new directory. This requires the -r flag, meaning recursive. This essentially means that for every element in the directory, we recursively copy those files to the new location. Flags can often be added to a command to provide additional information or for greater ease of use.
cp -r MyDirectory ../This command copies everything that exists in MyDirectory recursively and moves it back one directory from the current working directory.
Deleting Files
You must be extremely careful when deleting files through terminal. Unlike when you are using your general user interface and click and drag something to the trash, files cannot easily be restored. Never remove something without being absolutely certain it is what you intend to do and that there are no unintended consequences of its deletion.
The removal command is rm. Just like with cp, this can include the -r flag or not to be done recursively.
rm myfileThis code simply deletes the file myfile.
rm -r MyDirectoryThis deletes everything that is in the directory MyDirectory recursively.
Determining What is in a Directory
The list command lists all of the files that are in your current working directory.
lsYou can get more information about different files by using different flags. For instance, the flag -l gives additional information about your read/write access for a file, when it was last created/manipulated, who has access to it, the owner, etc.
ls -lRunning just ls tells you what is in your current working directory, but you can provide a path to determine what is in another directory. The following lists everything that is in MyDirectory, where ~ just indicates the root:
ls ~/MyDirectory/