All Linux commands and file names are case sensitive (ls does not equal LS). This is true for your username and password too!

Frequently used Linux commands are listed in the table below. Parameters listed in < > are REQUIRED, parameters listed in [ ] are OPTIONAL

Additional help is always available online by typing man <command> for each of the commands listed below. The behavior of most commands may be modified by adding options as illustrated under the ls command. This is only a quick command summary guide. Refer to the man pages for more detailed information.

Rivanna has a nice Linux introduction as well: https://arcs.virginia.edu/UNIX-tutorials-for-beginners

Files and Directories

ls [options] [directory]

List files. If no directory is given, files in the current directory are listed.

Examples:

ls            # Lists files in current directories
ls lab1*      # Lists files in the current directory beginning with the string lab1
ls -l         # Gives a long listing showing file attributes and file access permissions
ls -t         # Lists files starting with newest files first
ls subdir     # Lists files in directory subdir 
              # (where subdir is under the current directory) 

pwd

Print working directory: displays the name of the current directory.

cd [directory]

Change directory: change your default directory

Example:

cd                # change directory to your home directory 
cd ..             # move up one directory 
cd lab1           # move down into directory lab1 from your current directory 
cd lab1/problem1  # move down into directory lab1/problem1 from your current directory 

rm <filename>

Remove command: used to delete files.

Example:

rm junk.dat    # deletes the file named junk.dat 
rm *           # deletes all files from your current directory (Be careful w/ this command!)

cp <sourcefile> <targetfile>

Copy command. Copies file sourcefile to target targetfile

Example:

cp file1 file2          # copy file1 to the a new file called file2.
cp file1 subdir/file1   # copy the file file1 into the subdirectory subdir. 
cp file1 subdir         # copy the file file1 into the subdirectory subdir,
                        # if subdir exists, else copy file1 to a file called subdir!

mv <source> <target>

Move or rename command. Renames sourcefile to targetfile.

Example:

mv my_file my_old_file       # rename the file my_file to my_old_file \\
mv file.dat subdir/file.dat  # move the file file.dat into the subdirectory subdir. \\
mv file.dat subdir           # move the file file.dat into the subdirectory subdir, 
                             # if subdir exists, else rename file.dat to subdir!

mkdir <dirname>

Make directory. Creates a new directory.

Example:

mkdir test           # makes a directory called test under the current directory \\
mkdir test/first     # makes a directory called test/first under the current directory 
                     # (the test directory must already exist for this command to work)
mkdir -p test/first  # makes a directory called test/first under the current directory ,
                     # if test does not exist, create this directory as well

rmdir <dirname>

Remove directory. Remove a directory. The directory must be empty before it can be removed.

Example:

rmdir test    # removes directory named test from the current directory

chmod <mode> <filename>

Change the mode for a file. Each file on a LINUX system has permissions such as read, write, and execute. Doing an ls -l will list files along with their permission settings. Permissions may be set for three different kinds of users: the file owner/user (u), group members (g), or other users (o).

Examples:

chmod og-r file.txt   # disallow reading of file.txt by others/group members.
chmod og+r file.txt   # allow reading of file.txt by others/group members.
chmod o+x file.exe    # allow execution of file.exe by other users. 

# chmod may be used on directories as well to allow or to prevent access to the files stored there.
chmod o-x file.exe    # disallow other users from entering this directory.  

View/Search Text

cat <filename>

Prints the contents of a file to the screen.

more <filename>

less <filename>

more/less are used to display a text file on your screen on page at a time. To 1st order they are interchangeable, but less displays the results of your searches more clearly.

While viewing the file press:

return to advance the display by one line
space to advance the file by one screen full
b to go back by one screen full
/ to search. After pressing /, enter the text you wish to search for and hit return. Hitting / and return again will find the next occurrence of your text. less is better here, because it will highlight the found text. q to quit

grep <text> <filename>

Grep will print out all occurrences of the string text in the file filename.

head <filename>

Print the first 10 (default) lines of the file filename.

Examples:

head file.txt         # prints the 1st 10 lines of file.txt to the screen
head -n 20 file.txt   # prints the 1st 20 lines of file.txt to the screen

tail <filaname>

Print the last 10 (default) lines of the file filename.

Examples:

tail file.txt         # prints the last 10 lines of file.txt to the screen
tail -n 20 file.txt   # prints the last 20 lines of file.txt to the screen

Miscellaneous

ssh [-Y][-X][-l username] <host>

Open a secure shell connection to a remote host.

Example:

ssh -l myusername rivanna.hpc.virginia.edu Opens a secure connection to Rivanna.

passwd

Change your password. You will be prompted for your old and new passwords.

^C

To force a program to stop running, press Control-C (^C) in the terminal where the program was started.

logout

Log out from your LINUX account.

  • No labels