No Kind of Life Won't Be My Fault When You're Painted in the Corner of a No Good Life

17Jun/11

UNIX Command Line: Basics

Meant as a quick reference of basic commands for people who are new to using UNIX shell.

Click on any of the links below to quickly jump to that section of the page.
You may also hover your mouse over each link to see the full title of that section.


Directories | Navigation | Listing | Permissions | Moving | Editing


Directory Examples:

File and directory paths in UNIX use the forward slash "/" to separate directory names.

/ "root" directory
/usr directory usr (sub-directory of / "root" directory)
/usr/SUBDIR SUBDIR is a subdirectory of /usr

Navigating the File System:

pwd Display the "present working directory", or current directory.
cd Change current directory to your HOME directory.
cd /usr/SUBDIR Change current directory to your HOME directory.
cd INIT Change current directory to INIT which is a sub-directory of the current directory.
cd .. Change current directory to the parent directory of the current directory.
cd $THISVARIES Change current directory to the one defined by the environment variable 'THISVARIES'.
cd ~trin Change the current directory to the user trin's home directory (if you have permission).

Listing Directory Contents:

ls List a directory.
ls -l List a directory in long (detailed) format.
ls -a List the current directory including hidden files. Hidden files start with "."
ls -ld * List all the file and directory names in the current directory using long format. Without the "d" option, ls would list the contents of any sub-directory of the current. With the "d" option, ls just lists them like regular files.

Changing File Permissions and Attributes:

You must be the owner of the file/directory or be root before you can do any of these things.

chmod 755 file Changes the permissions of file to be rwx for the owner, and rx for the group and the world. (7 = rwx = 111 binary. 5 = r-x = 101 binary)
chgrp user file Makes file belong to the group user.
chown trin file Makes trin the owner of file.
chown -R cliff dir Makes cliff the owner of dir and everything in its directory tree.

Moving, Renaming, and Copying Files:

cp file1 file2 Copy a file.
mv file1 newname Move or rename a file.
mv file1 ~/SUBDIR/ Move file1 into sub-directory SUBDIR in your home directory.
rm file1 [file2 ...] Remove or delete a file.
rm -r dir1 [dir2...] Recursivly remove a directory and its contents. BE CAREFUL!
mkdir dir1 [dir2...] Create directories.
mkdir -p dirpath Create the directory dirpath, including all implied directories in the path.
rmdir dir1 [dir2...] Remove an empty directory.

Viewing and Editing Files:

cat filename Dump a file to the screen in ASCII.
more filename Progressively dump a file to the screen: ENTER = One Line Down / SPACEBAR = Page Down / Q = Quit
less filename Like more, but you can use Page-Up too. Not on all systems.
vi filename Edit a file using the VI editor. All UNIX systems will have VI in some form.
emacs filename Edit a file using the Emacs editor. Not all systems will have Emacs.
nano filename Edit a file using the Nano editor. Not all systems will have Nano.
head filename Show the first few lines of a file.
head -n filename Show the first n lines of a file.
tail filename Show the last few lines of a file.
tail -n filename Show the last n lines of a file.

A portion of this reference was pulled from Learn UNIX in 10 minutes on FreeEngineer.org.

Stay tuned for future updates and additions to this page!

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.