By: Tony Andrews. Linux directory ordering system Navigating and creating directories ◦ Listing...

21
Basic Unix/Linux Commands By: Tony Andrews

Transcript of By: Tony Andrews. Linux directory ordering system Navigating and creating directories ◦ Listing...

Page 1: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

Basic Unix/Linux Commands

By: Tony Andrews

Page 2: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

Linux directory ordering system Navigating and creating directories

◦ Listing directories and files ◦ Creating directories◦ Changing directories◦ Removing directories

Managing files ◦ Displaying content of a file◦ Copying files ◦ Moving files ◦ Removing files ◦ Searching the content of a file

Outline

Page 3: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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

Outline

Page 4: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

Linux Directory Ordering System 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)

Page 5: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.)

Navigating and Creating Directories

(stonebank)

Page 6: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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 ..”

Navigating and Creating Directories

(Stonebank)

Page 7: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.)

Navigating and Creating Directories

(Stonebank)

Page 8: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.

Navigating and Creating Directories

(Stonebank)

Page 9: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.

Managing files

(Stonebank)

Page 10: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

◦ 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

Managing files

Page 11: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

◦ 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/”

Managing files

(Stonebank)

Page 12: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.

Managing files

(Stonebank)

Page 13: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.

Managing files

(Stonebank)

Page 14: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.

Wildcards and general searches

(Stonebank)

Page 15: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.

Access Rights to Files

(Stonebank)

Page 16: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.

Access Rights to Files

(Stonebank)

Page 17: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.)

Processes

(Stonebank)

Page 18: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

To kill a process determine the PID by entering the command “ps”, then “kill PID” ◦ Ex.)

Processes

Page 19: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.)

Finding More Help With Commands

Page 20: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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.

Finding More Help With Commands

(Stonebank)

Page 21: By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.

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/basic-unix.html>.

Works Cited