Module 3 Manipulating Files and Directories The cp

  • Slides: 34
Download presentation
Module 3 Manipulating Files and Directories

Module 3 Manipulating Files and Directories

The cp command • Used to copy files and Directories • To copy a

The cp command • Used to copy files and Directories • To copy a single file: # cp file. txt /tmp • To copy and rename a file: # cp file. txt /tmp/new-file. txt • To copy a directory (Recursively) # cp -r /tmp /home/avi

Cutting, Pasting and Renaming with mv • The mv command can be used for

Cutting, Pasting and Renaming with mv • The mv command can be used for both cut-paste and for renaming purposes. • Renaming: # mv file. txt new-file. txt (in the same directory) • Cut and Paste: # mv /tmp /home/avi • Cut, paste and rename: # mv /tmp /home/avi/old-tmp • Please remember that when using the mv command with directories we do not use the -r option.

Deleting files and directories with rm • To delete a single file: # rm

Deleting files and directories with rm • To delete a single file: # rm file. txt • To delete a directory: # rm -r /home/avi/Downloads • Deleting with interaction question: # rm -ir /home/avi/Downloads • Ignoring Interaction: # rm -rf /home/avi/Downloads

Creating new directories with mkdir • To create a new directory: # mkdir ort-dir

Creating new directories with mkdir • To create a new directory: # mkdir ort-dir • To create a new directory path: # mkdir -p /home/avi/ort/public/documents • What will happen here ? # mkdir -p ~/ort{1, 2, 3, 4}/linux{a, b, c, d}/doc{1, 2, 3, 4}

Creating a new empty file and an instant file • Use the touch and

Creating a new empty file and an instant file • Use the touch and tee command to create a new empty file: # touch ~/new-file. txt • The touch command can also be used to change a file time stamp • Use the tee command to simultaneously write to screen and to a file: # tee ort. txt Hello Class Use ^D to end writing to file and return to shell

Looking inside a file • As Linux is a TUI based OS we need

Looking inside a file • As Linux is a TUI based OS we need the ability to look inside files without editing them: • The cat command display the file on the screen (standard output) # cat /etc/passwd • the more and less commands: # more /etc/httpd/conf/httpd. conf # less /etc/httpd/conf/httpd. conf

Looking at top and bottom of a file • Use the head command to

Looking at top and bottom of a file • Use the head command to look at the top of a file: # head -3 /etc/passwd • Use tail command to look at the bottom of a file: # tail -8 /etc/passwd • Use the tail command to get a “live” look at a file: # tail -f /var/log/messages

The wc command • Displays line, word, or character count of a file •

The wc command • Displays line, word, or character count of a file • Command format: wc [ -lwc ] filename(s) • Use the following options: -l Count lines -w Count words -c Count bytes • Examples: # wc /etc/passwd 39 264 1421 caesar. txt # wc –l /etc/passwd 39 caesar. txt

Linux Wildcards • Without these cool little things called shell wildcards, working on the

Linux Wildcards • Without these cool little things called shell wildcards, working on the Linux command line is pretty painful. Wildcard Matches * zero or more characters ? exactly one character [abcde] exactly one character listed [a-e] exactly one character in the given range [!abcde] any character that is not listed [!a-e] any character that is not in the given range {yes, no} exactly one entire word in the options given

Wildcard examples • the following removes every file from the current directory: # rm

Wildcard examples • the following removes every file from the current directory: # rm * • The following command moves all the HTML files, that have the word "linux" in their names, from the working directory into a directory named dir 1: # mv *linux*. html dir 1

Wildcard examples The following displays all files that begin with d and end with.

Wildcard examples The following displays all files that begin with d and end with. txt: # less d*. txt The following command removes all files whose names begin with junk. , followed by exactly three characters: # rm junk. ? ? ?

Wildcard examples With this command you list all files or directories whose names begin

Wildcard examples With this command you list all files or directories whose names begin with ort, followed by exactly one numeral: # ls ort[0 -9] This lists all files or directories beginning with ort, followed by exactly two numerals: # ls ort[0 -9]

Wildcard examples The following lists all files or directories whose name starts with either

Wildcard examples The following lists all files or directories whose name starts with either ort or linux, followed by any single character between a and c: $ ls {ort, linux}[a-c] This command copies all files, that begin with an uppercase letter, to directory dir 2: $ cp [A-Z]* dir 2 This deletes all files that don't end with c, e, h or g: $ rm *[!cehg]

Symbolic links • Symbolic links (soft) are used for two main reasons: • To

Symbolic links • Symbolic links (soft) are used for two main reasons: • To link a file or directory across file systems. • Backwards compatibility – when a command name, or file name changes, symbolic links are often used to refer to new files by both old and new names. • Symbolic links, unlike hard links, can cross file system boundaries, and can link directories. • To create symbolic links use the command: # ln –s filename linkname

Hard Links • A hard link is merely an additional name for an existing

Hard Links • A hard link is merely an additional name for an existing file on Linux or other Unix-like operating systems. • Hard links cannot link directories. • Cannot cross file system boundaries. Use the ln command: # ln file-name hard-link-name • Watch the inode number with: # ls -i

Exercise 1. 2. 3. 4. 5. 6. 7. Copy the etc directory to your

Exercise 1. 2. 3. 4. 5. 6. 7. Copy the etc directory to your home folder. Create a shortcut from the tmp directory to your home folder. In your copy of etc directory create a hard link to passwd Rename the etc directory (in your home folder) to my_etc How many lines are in ~/my_etc/httpd/conf/httpd. conf Create a new directory with your name in your home folder Create a new file under the directory with your name and in that file write down your name and family name 8. Create a new directory path under your home folder containing 5 different directories 9. Create a hard link from /tmp to your home directory. Is it possible ? 10. how can you find your PATH environment variable ? 11. how can you change your PATH environment variable ?

Exercise 1. Find all files and directories under my_etc ending with a dot (.

Exercise 1. Find all files and directories under my_etc ending with a dot (. ) and one character. 2. Try the following commands under my_etc: # more [A-Z]* my_etc # ls –ld my_etc/[!a-m]* # file /usr/bin/*x* (use exact path) # ls –ld /my_etc/[a-z]*[0 -9] # ls -a my_etc/. [!. ]*

The VI editor • • Introduction to vi The vi command vi modes Command

The VI editor • • Introduction to vi The vi command vi modes Command Mode Insert Mode Execution Mode Last Line Mode

Introduction to VI • vi is an interactive editor that is used to create

Introduction to VI • vi is an interactive editor that is used to create and/or change text files. • vi is a standard Unix editor and is cross platform. • Alternatives include emacs, pico, nano, xedit, gedit • vi is old in concepts, and hard to master, but once mastered it is a very powerful and fast editing tool • In some systems, especially for administrators, vi is the only option • The Linux version vim acts very similar to windows’ notepad, and is hence preferred in Linux

The VI Command • Start vi: $ vi filename(s) For full editing functionality $

The VI Command • Start vi: $ vi filename(s) For full editing functionality $ view filename(s) To open file(s) in read-only mode $ vi –r filename To recover a crashed file • vi will occupy the entire terminal screen. You will now be interacting with vi until you exit and return to the shell prompt • vi comes from old times, its commands actually belong to its predecessor ed, which was working on printer terminals, its mode of operation was: • Display a line • Run commands to change things in the line • Go to another line

VI Modes • Three modes in VI: 1. Command Mode 2. Insert Mode 3.

VI Modes • Three modes in VI: 1. Command Mode 2. Insert Mode 3. Execution Mode

VI Command Mode • When we open VI for editing we find ourselves in

VI Command Mode • When we open VI for editing we find ourselves in command-mode. • The keyboard acts differently but we can type-in commnds • To return to the command mode all we need to do is press ESC • If you are lost always press ESC to return to command-mode

VI Command Mode • In order to make sure that you are in command

VI Command Mode • In order to make sure that you are in command mode press ESC. • Basic command format: count command scope 10 dw The Above command will delete 10 next words from the cursor location

VI Command Mode • x delete character under cursor • X delete character to

VI Command Mode • x delete character under cursor • X delete character to left of the cursor • The d command deletes to buffer (like cut in word). • dd Deletes complete line • dw Deletes to the start of the next word • d 3 l Deletes the next three characters • d 0 Deletes to the beginning of the line • d$ Deletes to the end of the line • d 1 G Deletes to the beginning of the file • d. G Deletes to the end of the file • D Deletes to the end of the line

VI command Mode • Cut, Copy and Paste Commands • yy Yank a complete

VI command Mode • Cut, Copy and Paste Commands • yy Yank a complete line • 20 yy Yank 20 complete lines • dd cut a line • 5 dd cut 5 lines • P paste above you • p paste under you

VI command Mode • u • G • 1 G • 212 G •

VI command Mode • u • G • 1 G • 212 G • J • . undo go to end of file go to first line go to line 212 Join current and next line Repeat last command

VI Insert mode • In this mode the VI starts acting like a regular

VI Insert mode • In this mode the VI starts acting like a regular notepad. • To leave the insert mode press ESC (back to command mode) • To jump from command to insert mode you can: # press the Insert button And. . A , a , I , i , O , o

Search and Replace • Search Only: • /pattern Search forward for patterns • ?

Search and Replace • Search Only: • /pattern Search forward for patterns • ? pattern Search backwards for pattern • n Repeat last search • N Repeat last search in the opposite direction

Search and Replace (in execution : ) • : 1, $s/old/new/g Replaces all old

Search and Replace (in execution : ) • : 1, $s/old/new/g Replaces all old with new all over the file • : 1, 20 s/old/new/gc Replaces all old with new between lines 1 -20 and asks for confirmation before changing. • : n, ms/old/new/g replaces all old with new between lines n-m

Execution Mode : • : r file • : w! • : w file

Execution Mode : • : r file • : w! • : w file • : wq • : x • : q! • : !cmd • : r!cmd the file Read file and place its contents after the line Write – save changes Write file overriding protection mode Write to named file Write and quit (exit) Quit Force quit without saving changes Execute shell command and insert the output to

VI options • vi has many options to control behavior. • To list available

VI options • vi has many options to control behavior. • To list available options and their status, type: : set all • To enable an options (for example line numbering), type: : set number - Will show line numbers : set showmode – Will show when we are in INSERT mode • To disable an option (for example line numbering), type: : set nonumber : set noshowmode • In vim there are many more commands. One very recommended is: : syntax on - Which will use colors to make the edited file more readable, according to its type - Useful for editing C files and bash scripts for example (requires syntax files to be installed )

<Insert Picture Here>

<Insert Picture Here>