-
Linux uses a tree structure where you have files and folders (directories). In
the description below, we use % as command prompt.
-
After logging on you will be in "your home directory".
-
To list (display) the contents of a directory use the ls command.
Some examples:
% ls /* Files that start with a period (e.g., .aliases) are not listed */
% ls -al /* Long listing that also displays hidden files (those starting with a period) */
% ls -F /* Adds forward slash to identify directories */
-
To change to a directory use the cd command. Three important
directories:
- . (single period) → Represents the current directory.
- .. (two periods) → Represents the parent directory.
- ~ (tilde) → Represents your home directory.
Examples:
% cd data_dir
% cd /* Takes you back to your home directory; great when you are lost */
% cd / /* Takes you to the root directory (under which all files/folder reside) */
% cd .. /* Takes you to the parent directory */
% cd ~/216public /* Takes you to the 216public directory that resides in the home directory */
-
To create a directory use the mkdir command. For example:
% mkdir data_dir
-
To display the directory where you find yourself in use the pwd command. For example:
% pwd
-
There are several text editors (emacs, vim, etc.) A very simple one is nano. Once
you start nano, look at the bottom of the window to see a summary of commands.
% nano my_prog.c
-
To copy files use the cp command. For example:
% cp my_prog.c my_copy.c
% cp my_prog.c data_dir /* Copies my_prog.c to the data_dir directory */
% cp my_prog.c .. /* Copies my_prog.c to the parent directory */
% cp /tmp/e.txt . /* Copies e.txt to current directory. Notice the period after e.txt */
% cp -r Week1 ~/216 /* Copies the directory Week1 to the folder 216 in the home directory */
-
To remove a file use the rm command. For example:
% rm my_prog.c
-
To remove a directory (it must not have any files) use the rmdir command. For example:
% rmdir data_dir
-
To remove a directory (with files) use the rm command. For example:
% rm -f -r Week1
-
To rename a file use the mv command. For example:
% mv my_prog.c my_prog_new_name.c
-
To move a file to a directory use the mv command. For example:
% mv my_prog.c data_dir
-
To display the contents of a file without opening with an editor use the less command. For example:
% less my_prog.c
-
There are several ways to change file permissions using the chmod. For example:
% chmod 400 data.txt /* Only the owner (you) can read it */
% chmod 600 data.txt /* Only the owner (you) can read and write it */
% chmod 700 data.txt /* Only the owner (you) can read, write, and execute it */
Additional information about chmod can be found at
File Directory Permissions.
-
To log out use the logout command. For example:
% logout
-
To access Unix manual pages use the man command. For example to
find information about the ls command:
% man ls
-
To compare file use the diff command. For example:
% diff file1 file2
-
To create a compressed zip archive file use the zip command.
For example, for compressing a set of files:
% zip mydata.zip data1.txt data2.txt
For compressing a directory (e.g., material):
% zip -r material.zip material
To extract files:
% unzip mydata.zip
-
To create a compressed gzip archive file use the tar command.
For example, creating .tar.gz file for directory grep_example:
% tar czf grep_example.tar.gz grep_example
-
To uncompress a .tar.gz file use the tar command.
% tar xzf grep_example.tar.gz
-
To create a symbolic link to a file or directory use the ln command.
For example to create a symbolic link in the current directory to the directory
tasks that is in the tmp directory:
% ln -s tmp/tasks
-
To find a file use the find command. After find,
specify the directory and after -name the filename or matching expression (e.g., *.txt).
find . -name a.out -print
-
To find lines with a pattern in a file(s) you can use the
grep command. For example, to find lines
having the word TODO in all .c files of the current
directory, you can execute the command:
grep TODO *.c
-
You can transfer files between computers by using the
scp command. For example, to transfer
to your computer a file named test.txt present in
the home directory of a user with user id
terpito (in the computer/system/host named grace.umd.edu)
you can execute the command below.
In a Mac you can open a terminal in order to access scp.
In a PC, search for the cmd application, and once started it
you will have a command line terminal with access to scp.
scp terpito@grace.umd.edu:~/test.txt .
Here is another example where we want to transfer
the folder ~216public/projects/project1 present in the account of user terpito
(in the grace.umd.edu host) to your current folder (notice the period at the end):
scp -r terpito@grace.umd.edu:~/216public/projects/project1 .
In the following example, we are transfering a folder to the ~/216 folder of
the user terpito in the grace.umd.edu host:
scp -r sample_folder nelson@grace.umd.edu:~/216
In both examples above, you can drop the -r option if you are transferring a single file.
There are graphical programs that allow you to transfer files. See
https://www.cs.umd.edu/~nelson/classes/resources/file_transfer/
for additional information.
-
You can find additional information about a command by using
the man command. For example, to find
additional information about grep:
man grep
-
If you want to transfer files from grace.umd.edu to your computer
you can use https://dav.terpconnect.umd.edu/.
Use your directory id and password to connect.
- Additional Commands
% date /* Current date time */
% who /* Current users */
% ps /* Shows processes */
% emacs my_prog.c /* Popular editor */
% ls -al | less /* Using | to pipe output of one command as input to another */
% cal /* Calendar */
% ls *.txt /* Lists all files ending in .txt */
% cat data.txt /* Displays contents of a file (in this case data.txt) */