Command Line Basics
Last updated
Last updated
First, we will use the pwd (print working directory) command to show our current location.
We can also list the files in out current working directory.
To move to the demo_files sub-directory, we can use the cd (change directory) command. We can follow this up with the pwd command to verify we have moved to the demo_files sub-directory.
We can also move backward in our directory line using the cd .. command. Here we will move from the demo_files back to the Joseph.Madison directory.
Next, we will make two new directories in the Joseph.Madison directory. We can do this using the mkdir command. We will call our directories exp1 and exp2. Verifying that the two new directories were created can be done by using ls.
Next, we will make a text file in the exp1 directory. We first have to cd into the exp1 directory. It is again good practice to verify your location with pwd.
Next, we will create a file using a text editor. Here we will use nano (there are others including emacs and vi, but nano is simple). We use the nano command followed by the name of what we want to call the new file. (Include a file extension if desired.)
Once you press enter you will be moved into the text editor. A cursor at the top of the page is where text will be entered as you type. This is a powerful tool with many applications but here I will type 'Hello World!'
I will then use ctrl-X to exit. This will give me a prompt for saving changes.
Type y for yes. You will then be prompted to verify the file name, hit enter or change the original name and hit enter.
You will then be brought back to the directory you put the file in. You can verify your location with pwd.
You can also move the file you just made with the mv command. Here, I will move file1 from the exp1 directory to the exp2 directory. I will also change directories to the exp2 directory and verify my file was moved. Note: You must specify the entirety of the directory you are moving to, otherwise your file will just be renamed (mv can double over as a rename command).
If you want to copy a file instead of moving it, you can use the cp command in the same manner.
The file is now in both locations.
Another command that is very useful and very dangerous is the remove command rm.
The HPC does not backup files so if you remove a file or directory it is gone (always have backup files elsewhere such as on your local PC!) Here I will remove file1 from exp2. You can ls and nothing should be there (you will be sent back to the command prompt.
Directories can also be removed using the rm -r command. This command will also delete any files contained in the directory. You first have to move the parent directory. After deleting you can verify it is no longer there using ls.
Another useful command is the whoami command. This command will tell you what user you are on the system (and is also a nice sanity check late at night)
The last subject of discussion in this doc will be the echo command. The echo command can serve many functions such as calling shell and environment variable values. Here it is demonstrated by simply repeating what we enter.
Command
Description
pwd
print working directory (current location)
ls
list directories & files in current directory
cd ..
back up one directory
cd
back up to the home directory
cd myDirectory/
open myDirectory
mkdir exp1
makes a directory-exp1
nano file1
opens file1 in nano editor
cp file1 /home/User.name/exp2
copies file to exp2 (current location must be
the original location of file1)
mv file1 /home/User.name/exp2
copies file to exp2 (current location must be
the original location of file1)
mv file1 ./exp1 ./exp2
moves file1 from exp1 to exp2 (exp1 & exp2
are both in the current directory (denoted by period (.))
rm file1
removes file1 from current directory
whoami
returns username
echo Hello World
prints Hello World
to the terminal