Basic_Linux_commands_Tony_Andrews_2011

advertisement
By: Tony Andrews



Linux directory ordering system
Navigating and creating directories
◦
◦
◦
◦
Listing directories and files
Creating directories
Changing directories
Removing directories
◦
◦
◦
◦
◦
Displaying content of a file
Copying files
Moving files
Removing files
Searching the content of a file
Managing files

Wildcards and general searches
◦ Wildcard “*” and “?”
◦ Find


Access rights to files
Processes
◦ Listing processes
◦ Killing processes

Finding more help with commands in command
window.
◦ Manual
◦ Whatis
◦ Apropos

The linux operating system is built like a tree.
◦ Top directory is the root directory “/”
◦ Followed by other ones like home etc.
◦ Music is located at: /Home/Tony/Music
(Stonebank)

Command “ls” lists all of the files and
directories in the current directory.
◦ Ie.)


The command “ls –a” lists all of the files and
some hidden files that start with “.” or “..”
Command “ls –l” is a long list which shows all
of the files and directories as well as how
large they are and when they were created.
◦ Ie.)
(stonebank)

The command “mkdir name” creates a directory
within the current directory.
◦ Ex.) “mkdir oldmusic” creates the directory called
“oldmusic” within the current directory.


The command “cd .” stays in the current
directory.
The command “cd dirname” changes to a new
directory within the current one.
◦ Ex.)
◦ To move back up from the “oldmusic” directory to the
directory before it enter the command “cd ..”
(Stonebank)

To list the current pathway in the directory
tree use the command “pwd”.
◦ Ex.)
◦ To navigate to any directory in the tree enter the
command: “cd ~/name(1)/name(2)/…/name(n)”
 Ex.)
(Stonebank)

Once a directory is created, it can be removed
by using “rmdir”, however the directory has to
be empty first.
◦ Multiple directories can be removed by using
spaces between each directory.
 Ex.) “rmdir directory1 directory2” removes both
directory 1 and 2.
(Stonebank)

Displaying content of a file in the command
window can be done a few different ways.
◦ To display the entire file type: “cat filename”
 For example, “cat science.txt” would display the whole
science.txt document.
 Kind of hard to read.
◦ To have the file display better on the command
window type: “less filename”
 “less science.txt” would display science.txt in a way
that is easier to see in the window.
 Instead of using the arrow keys or a scroll on a mouse
you press the space bar and get out of the document
view by pressing the Q key.
(Stonebank)
◦ The “head file” command shows the first ten lines of a
file and the “tail file” command shows the last ten lines
of a file.
 The number of lines shown for both commands can be
specified by using “head –n file” or “tail –n file” where n is
any number less than or equal to the number of lines in the
file.
 Multiple files can be seen at the same time.
 Ex.) Want to display the first 16 lines of 2 separate
documents “science.txt” and “zebras.txt”. Type in:
 “head -16 science.txt zebras.txt”. Both documents with 16
lines in the beginning of each will be the output.
◦ All of these displaying commands only work if the files
are in the same directory.

Copying files
◦ To copy a file use the “cp” command
◦ When a file is copied within the same directory, it is
saved under a different file name but has the same
content, and the original is still in the directory with it.
 Ex.) “cp file1 file2” copies the content of file1 into a new
file2.
◦ To copy a file to a new directory type:
 “cp file1 directory/”

Moving files
◦ To move a file use: “mv” command.
◦ Moving a file within the same directory is another way of
renaming a file.
◦ To move a file to another directory type:
 “mv file1 directory/”
(Stonebank)

Removing files
◦ Use the command “rm”.
 Ex.) “rm file1” removes file1 from the system.

Searching files
◦ To search a given file for any keyword type: “grep
‘keyword’ file”
 Ex.) Typing “grep science science.txt” outputs all of the lines
of the document which contain the word science with s in a
lowercase.
 “grep” can be combined with –i, -v, -n, or –c to narrow the
search.
 Typing “grep –i ‘keyword’ file” ignores whether keyword is
uppercase or not and outputs all the lines which contain the
word.
(Stonebank)
 Typing “grep –v ‘keyword’ file” ignores the keyword
and outputs all lines without it.
 “grep –n ‘keyword’ file” gives the line number and the
line with the keyword.
 Finally “grep –c ‘keyword’ file” gives the total number
of lines with the keyword.
◦ “grep” can be combined with some or all of the
letters to narrow the search more.
 Ex.) “grep –ivc science science.txt” ignores all instances
of the word science with any case and produces the
total number of lines in the document without the
word science.
(Stonebank)

Wildcards are used with list commands to search
for specific files in a directory.
◦ Wildcard “*” is used before or after a filename.
 Ex.) “ls *ide” lists all of the instances of files ending with ide,
and “ls ide*” lists all files beginning with ide.
◦ Wildcard “?” is used in a keyword to represent one letter
out of that word.
 Ex.) “ls ?one” lists all files that have 4 letters ending in “one”
like bone but not drone or stone.

The command “find” is a general search of the
directories.
◦ Ex.) find . -name "*.txt" –print
 starts with the current directory ( . ) and then moves through
the lower directories to find any file than ends in .txt and
lists them in the command window.
(Stonebank)


A file is made with certain access rights, which
means that one can either read, write, or execute
it. However, some files are programed in such a
way that they can’t be tampered with.
The access rights are shown by using the
command: “ls –l”
◦ The output code describing the access rights is given on
the far left of the output
 Ex.) The output for a file called “biglist.txt” is
 -rw-rw-rw-. 1 tony tony 68 Jan 31 21:57 biglist.txt
 -rw-rw-rw- is the access rights for the file, the left most space
indicates whether or not it is a file, directory, or something else.
The 9 spaces after that indicate access rights for the user,
group, or any others that have access to the file.
(Stonebank)

The “chmod” command allows you to
customize access rights for a file.
◦ Ex.) “chmod go-rwx biglist.txt” means that groups
or others can’t read or write the file biglist.txt.
◦ The negative sign before rwx removed the read,
write, and execute access rights for groups and
others. But left the user rights unchanged.
◦ Putting a positive sign before rwx would add those
rights for groups and others.
(Stonebank)

The command “ps” lists all the processes that
are running either in the foreground or
background.
◦ It lists things like the process identification number
(PID) on the far left side and the commands on the
far right hand side for each process
 Ex.)
(Stonebank)

To kill a process determine the PID by
entering the command “ps”, then “kill PID”
◦ Ex.)

The command “man ‘commandname’” gives a
manual for any command. It gives a couple
paragraphs on what the command is and
common uses.
◦ Ex.) “man cp” gives the manual for the copy
command.

The command “whatis ‘command’” gives a
couple uses for the command and short
descriptions for each command.
◦ Ex.)

The command “apropos ‘keyword’” can be
used if you forget a command.
◦ Ex.) “apropos move” gives all commands involving
the word move in a description.
(Stonebank)

Stonebank, Michael. "UNIX Tutorial for Beginners"
UNIX Tutorial for Beginners, 19 Oct 2001. Web. 2
Feb 2011.
<http://www.ee.surrey.ac.uk/Teaching/Unix/>.
riehemann, susanne. "Basic UNIX commands." Basic
UNIX commands. Computing Information for Stanford
Linguists , 07 Nov 2001. Web. 3 Feb 2011.
<http://mally.stanford.edu/~sr/computing/basicunix.html>.

Download