Linux Command Line Basics

From CSWiki

Jump to: navigation, search

Directories

Directories are the same as folders in Windows. Rather than drive letters, such as C:\, all Linux directores are under the "root directory" known simply as /. If you specify a directory starting with a /, this is called an "absolute path", meaning you are specifying exactly where to go.

If you specify a directory without a /, this is called a "relative path", meaning the directory will effectively be prepended with your current working directory. Example:

$ pwd
/home/ugrad/ajshafer
$ cd /home
$ pwd
/home
$ cd ugrad/ajshafer
$ pwd
/home/ugrad/ajshafer

Most shells also have shortcuts for getting to home directories:

$ cd ~
$ pwd
/home/ugrad/ajshafer
$ cd ~trthomps
$ pwd
/home/ugrad/trthomps

Since trthomps's home folder might not have been in /home/ugrad, this is very useful. You may specify any username in this fashion to reference their home folder.

There are also two special directories implicit in every directory: . and ... "dot" points to the current directory, "dot dot" points to the parent directory. Example:

$ pwd
/home/ugrad/ajshafer
$ cd ..
$ pwd
/home/ugrad
$ cd ..
$ pwd
/home
$ cd ugrad/ajshafer
$ ls
foo.txt
bar.txt
a.out
$ cat foo.txt
foo!
$ ./a.out
Hello, World!

The command cd .. will likely be the most useful thing to take away from this.

Linux Commands

Commands, like everything in Linux, are files. If you specify a command without any slashes in it, the system will search your $PATH and execute the first file that matches. You can $ echo $PATH to see which directories the system will search for a command you specify. You can use $ which <command> to see the absolute path of the <command> that would be run.

pwd print working directory. Tells you what directory you are working in right now. This is also usually shown in your bash prompt.
cd <directory> change directory. Changes your working directory to <directory> (replace with a directory)
ls List the contents of the current working directory
ls -lh List the contents of the current working directory, along with size, mode, access times, and other extra information.
rm <file> Delete <file> (Cannot be undone)
mv <oldfile> <newfile> Moves and/or renames <oldfile> to <newfile>
pico <file> A very simple command-line text editor. Opens <file> for editing.
grep <pattern> * Search all files in the current directory for the text <pattern>.
cat <file> Print out <file> to the terminal
less <file> Print out <file> to the terminal with arrow key scrolling. You may also type /<text> will the file is open to search the file for <text>.
tail <file> Print out the last 10 lines of <file> to the terminal
tar <see description> Compress/decompress files:

Compress <folder> to file.tar.gz: tar -czvf file.tar.gz <folder>
Compress <folder> to file.tar.bz2: tar-cjvf file.tar.bz2 <folder>
Decompress file.tar.gz to current folder: tar -xzvf file.tar.gz
Decompress file.tar.bz2 to current folder: tar-xjvf file.tar.bz2

screen See GNU Screen
man <command> Get the documentation for <command>
vim <file> An improved command-line text editor. See Vim

See Also

Linux File Permissions and Modes (Under Construction)
GNU Screen

Personal tools