Common UNIX Commands - for files and directories
This article is an adaptation of content published at UNIX/Linux: Getting Started and is published here under a Creative Commons Attribution 3.0 License.
File and directory names can be up to 256 characters long. They may contain upper and lower case characters (including letters, numbers, punctuation, and other symbols).
Note
Try to avoid using characters like "/", ">", "!", "?" or blank spaces in filenames, as they are sometimes hard to manipulate because of their special designations within the shell.
In UNIX, these are case sensitive. For instance, the name "Myxztplk.txt" refers to a file different file from "myxztplk.txt." The file hierarchy starts from the root (or "/") and makes its way down to the particular file name on the path. The full name for any file is the complete path to it, starting at the root, which means all the directories to it. The path name for the file "myxztplk.txt" in the directory "villains" in the directory "superman" which is under the root directory would be: /superman/villains/myxztplk.txt
Use the pwd command to list the name of your p rint w orking d irectory. You may refer to any files in your present directory without using their full path names. Often, you will not have a UNIX account that begins at the root directory. Most UNIX platforms have many users, and each user has a small slice of the available hard drive space. Thus, instead of starting out at the root of the drive, most user accounts will be somewhere down in the directory tree. [4]
Some common commands to help you interact with the filesystem and your files are:
ls (list): List directory contents
ls | lists the contents in a directory in columns |
ls -l | gives a longer/fuller listing including file permissions, size, date created (the "l" is for long) |
ls -la | similar to the above but includes "dot"/hidden files (the "a" is for all) |
cd (change directory): Move to a different directory
cd | returns you to your home directory |
cd ../ | moves up one directory level |
cd ../.. | moves up two directory levels |
cd mysubdirectory | moves to the subdirectory mysubdirectory |
cp (copy): Copy files and directories
cp file1.txt file2.txt | copies file1.txt to file2.txt |
cp file1.txt mydirectory/ | copies file1.txt to named mydirectory |
cp file1 file2.txt mydirectory/ | copies file1.txt and file2.txt to named mydirectory |
mv (move): Move or rename files or directories
mv file1.txt newname.txt | renames file1.txt to newname.txt |
mv mydirectory newname | renames directory to newname |
mv file1.txt directory/ | moves file1.txt to named directory |
mv file1.txt directory/newname.txt | moves file1.txt to named directory and renames it to newname.txt |
rm (remove): Delete files or directories
rm file1.txt | removes (deletes) file1.txt |
rm -i file1.txt | deletes named file1.txt after prompting to make sure you wish to remove it (the "i" is for interactive) |
Caution
BE EXTREMELY CAREFUL with rm . There is no undelete command!
mkdir (make directory): Create directories
mkdir mysubdirectory | creates a directory within the current directory called mysubdirectory |
rmdir (remove directory): Delete directories
rmdir mysubdirectory | removes (deletes) directory mysubdirectory within the current directory (if mysubdirectory is empty i.e. has no files in it) |
chmod (change mode): Change file or directory access permissions
chmod u+w file1.txt | adds write permission for the user to file1.txt ("write" is the ability to edit or delete the file) |
chmod g+x file1.txt | adds execute permission for the group to file1.txt |
chmod o-r file1.txt | removes read permission for the world (other) from file1.txt |
The chmod command assists in allowing you to share files with another user or group or to restrict access ("lock down") directories of files to keep things private. The document [ http://help.unc.edu/?id=217 ] How to Use UNIX and Linux File Permissions explains this in detail.
Note
Since UNC users' file space is sometimes in AFS (your home directory or any directory that starts with "/afs/") , [ http://help.unc.edu/?id=215 ] AFS ACLs (Access Control Lists) control directory access permissions as well as basic UNIX file permissions.
more: List/view files one screen at a time
more file1.txt | scrolls forward through file1.txt one screen at a time. Press spacebar to go forward one screen, b to go back one screen, or q to quit |
pwd (print working directory): Display path name of current directory
pwd | displays your current directory |
cat: Concatenate files and list them to the screen.
cat file1.txt | prints the contents of file1.txt to your screen |
cat file1.txt file2.txt | prints the contents of file1.txt and file2.txt to your screen |
cat file1.txt file2.txt > file3.txt | concatenates the contents of file1.txt and file2.txt into file3.txt |
diff: Compare two files and shows where they differ
diff file1.txt file2.txt | lists the differences between the files file1.txt and file2.txt |
wc: Count characters, words or lines
wc -c file1.txt | prints the number of bytes in file1.txt |
wc -l file1.txt | prints how many lines are in file1.txt |
who: List users logged in the machine
who | lists users who are logged in the machine |
who -m | shows you what the ip address of the machine you logged in from |
ps: Find out what jobs you are running and what shell you are in
ps | lists your current running jobs and your shell you are in |
ps -ef | lists jobs of others as well as yours |
As can be seen, all of these commands have optional flags and many take arguments, all of which can be found in the man pages.