LP Networking
LP Networking
home/ bin/
jeff/ cp rm
■ It’s common to put an extension , beginning with a dot, on the end of a filename
■ The extension can indicate the type of the file:
.txt Text file
.gif Graphics Interchange Format image
.jpg Joint Photographic Experts Group image
.mp3 MPEG-2 Layer 3 audio
.gz Compressed file
.tar Unix ‘tape archive’ file
.tar.gz, .tgz Compressed archive file
1
$ pushd ~fred
$ cd Work
$ ls
$ popd
■ popd takes you back to the directory where you last did pushd
■ dirs will list the directories you can pop back to
■ Modern shells help you type the names of files and directories by completing partial names
■ Type the start of the name (enough to make it unambiguous) and press Tab
■ For an ambiguous name (there are several possible completions), the shell can list the options:
● For Bash, type Tab twice in succession
● For C shells, type Ctrl+D
■ Both of these shells will automatically escape spaces and special characters in the filenames
$ rm -v data.?
Removing data.1
removing data.2
removing data.3
■ Note: wildcards are turned into filenames by the shell, so the program you pass them to can’t
tell that those names came from wildcard expansion
● Write a wildcard pattern that matches only the name you want to delete:
$ rm -i ./name-with-funny-characters*
● The ./ forces it to be in the current directory
● Using the -i option to rm makes sure that you won’t delete anything else by accident
3
4
1.12 Making Directories with mkdir command
■ Syntax: mkdir directory-
names
■ Options:
● -p, create intervening parent directories if they don’t already exist
● -m mode, set the access permissions to mode
■ For example, create a directory called mystuff in your home directory with permissions so that
only you can write, but eveyone can read it:
$ mkdir -p /tmp/one/two/three
■ The data in files comes in various different formats (executable programs, text files, etc.)
5
1.15 Changing Timestamps with touch command
■ Changes the access and modification times of files
1.16 Exercises
1. a. Use cd to go to your home directory, and create a new directory there called dog.
b. Create another directory within that one called cat, and another within that called mouse.
c. Remove all three directories. You can either remove them one at a time, or all at once.
d. If you can delete directories with rm -r, what is the point of using rmdir for empty directories?
2. a. Copy the file /etc/passwd to your home directory, and then use cat to see what’s in it.
c. Make a directory called programs and copy everything from /bin into it.
3. a. The touch command can be used to create new empty files. Try that now, picking a name for the new
file:
$ touch baked-beans
b. Get details about the file using the ls command:
$ ls -l baked-beans
c. Wait for a minute, and then try the previous two steps again, and see what changes. What happens
when we don’t specify a time to touch?
6
Module 2 : Work Effectively on the Linux Command
Line
2.1 Shells
■ A shell provides an interface between the user and the operating system kernel
User
Shell
Kernel
● Called builtins
● Only a small number of commands are builtins, most are separate programs
■ Afewprogramsusedifferentstylesofcommand-lineoptions
● For example, long options (not single letters) sometimes start with a single - rather
than –
2.6 Examples of Command-Line Options
■ List all the files in the current directory:
$ ls
■ List the files in the ‘long format’ (giving more information):
$ ls -l
■ List full information about some specific files:
$ ls -l notes.txt report.txt
■ List full information about all the .txt files:
$ ls -l *.txt
■ List all files in long format, even the hidden ones:
$ ls -l -a
$ ls -la
8
9
2.8 Environment Variables
■ Use the echo command with a $ sign before a varable name to see its value, e.g.
$ echo $PS1
[\u@\h \W]\$
■ The special characters \u, \h and \W represent shell variables containing, respectively, your
user/login name, machine’s hostname and current working directory, i.e.,
■ The event designator !$ refers to the last argument of the previous command:
$ ls -l long_file_name.html
-rw-r--r-- 1 jeff users 11170 Oct 31 10:47 long_file_name.html
$ rm !$
rm long_file_name.html
■ Similarly, !ˆ refers to the first argument
■ Acommandoftheformˆstring ˆreplacement ˆ replaces the first occurrence of string
with replacement in the previous command, and runs it:
$ echo $HOTSNAME
$ ˆTSˆSTˆ
echo $HOSTNAME
tiger
2.14 Summary of Bash Editing Keys
■ These are the basic editing commands by default:
● Right — move cursor to the right
● Left — move cursor to the left
● Up — previous history line
● Down — next history line
● Ctrl+A — move to start of line
● Ctrl+E — move to end of line
● Ctrl+D — delete current character
■ There are alternative keys, as for the Emacs editor, which can be more comfortable to use than
11
the cursor keys
■ There are other, less often used keys, which are documented in the bash man page (section
‘Readline’)
■ You can write multiple commands on one line by separating them with ;
■ For example, use the locate command to find all files called manual.html and print information
about them with ls:
$ ls -l $(locate manual.html)
$ ls -l ‘locate manual.html‘
■ The punctuation marks on the second form are opening single quote characters, called
backticks
● The $() form is usually preferred, but backticks are widely used
■ Line breaks in the output are converted to spaces
■ Another example: use vi to edit the last of the files found:
$ vi $(locate manual.html | tail -1)
12
■ The locate command is a simple and fast way to find files
2.22 Exercises
1. a. Use the df command to display the amount of used and available space on your hard drive.
b. Check the man page for df, and use it to find an option to the command which will display the free
space in a more human-friendly form. Try both the single-letter and long-style options.
c. Run the shell, bash, and see what happens. Remember that you were already running it to start with.
Try leaving the shell you have started with the exit command.
2. a. Try ls with the -a and -A options. What is the difference between them?
b. Write a for loop which goes through all the files in a directory and prints out their names with echo. If
you write the whole thing on one line, then it will be easy to repeat it using the command line history.
c. Change the loop so that it goes through the names of the people in the room (which needn’t be the
names of files) and print greetings to them.
d. Of course, a simpler way to print a list of filenames is echo *. Why might this be useful, when we
usually use the ls command?
3. a. Use the find command to list all the files and directories under your home directory. Try the -type d
and -type f criteria to show just files and just directories.
b. Use locate to find files whose name contains the string ‘ bashbug’. Try the same search with find,
looking over all files on the system. You’ll need to use the * wildcard at the end of the pattern to match
files with extensions.
14
15