0% found this document useful (0 votes)
26 views15 pages

LP Networking

The document provides an overview of basic file management and command line usage in Linux, covering topics such as filesystem objects, file and directory naming conventions, and common commands like cp, mv, and rm. It also discusses shell commands, command-line arguments, environment variables, and the use of history for command reuse. Exercises are included to reinforce learning about creating, copying, moving, and deleting files and directories.

Uploaded by

blackmonstercode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views15 pages

LP Networking

The document provides an overview of basic file management and command line usage in Linux, covering topics such as filesystem objects, file and directory naming conventions, and common commands like cp, mv, and rm. It also discusses shell commands, command-line arguments, environment variables, and the use of history for command reuse. Exercises are included to reinforce learning about creating, copying, moving, and deleting files and directories.

Uploaded by

blackmonstercode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Module 1 : Perform Basic File Management /Linux

1.1 Filesystem Objects


■ A file is a place to store data: a possibly-empty sequence of bytes
■ A directory is a collection of files and other directories
■ Directories are organized in a hierarchy, with the root directory at the top
■ The root directory is referred to as /
/

home/ bin/

jeff/ cp rm

1.2 Directory and File Names

■ Files and directories are organized into a filesystem


■ Refer to files in directories and sub-directories by separating their names with /, for example:
/bin/ls
/usr/share/dict/words
/home/jeff/recipe
■ Paths to files either start at / (absolute) or from some ‘current’ directory

1.3 File Extensions

■ 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

■ On Unix and Linux, file extensions are just a convention


● The kernel just treats them as a normal part of the name
● A few programs use extensions to determine the type of a file

1.4 Going Back to Previous Directories

■ The pushd command takes you to another directory, like cd


● But also saves the current directory, so that you can go back later
■ For example, to visit Fred’s home directory, and then go back to where you started from:

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

1.5 Filename Completion

■ 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

1.6 Wildcard Patterns


■ Give commands multiple files by specifying patterns
■ Use the symbol * to match any part of a filename:
$ ls *.txt
accounts.txt letter.txt report.txt
■ Just * produces the names of all files in the current directory ■
The wildcard ? matches exactly one character:

$ 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

1.7 Copying Files with cp command


■ Syntax: cp [options] source-file destination-file
■ Copy multiple files into a directory: cp files
directory ■ Common options:
● -f, force overwriting of destination files
● -i, interactively prompt before overwriting files
● -a, archive, copy the contents of directories recursively

1.8 Examples of cp command


■ Copy /etc/smb.conf to the current directory:
$ cp /etc/smb.conf .
■ Create an identical copy of a directory called work, and call it work-backup:
2
$ cp -a work work-backup
■ Copy all the GIF and JPEG images in the current directory into images:
$ cp *.gif *.jpeg images/

1.9 Moving Files with mv command


■ mv can rename files or directories, or move them to different directories

■ It is equivalent to copying and then deleting


● But is usually much faster
■ Options:
● -f, force overwrite, even if target already exists
● -i, ask user interactively before overwriting files
■ For example, to rename poetry.txt to poems.txt:
$ mv poetry.txt poems.txt
■ To move everything in the current directory somewhere else:
$ mv * ˜/old-stuff/

1.10 Deleting Files with rm command


■ rm deletes (‘removes’) the specified files
■ You must have write permission for the directory the file is in to remove it
■ Use carefully if you are logged in as root!
■ Options:
● -f, delete write-protected files without prompting
● -i, interactive — ask the user before deleting files
● -r, recursively delete files and directories
■ For example, clean out everything in /tmp, without prompting to delete each file:
$ rm -rf /tmp/*

1.11 Deleting Files with Peculiar Names

■ Some files have names which make them hard to delete

■ Files that begin with a minus sign:


$ rm ./-filename
$ rm -- -filename
■ Files that contain peculiar characters — perhaps characters that you can’t actually type on your
keyboard:

● 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 -m 755 ~/mystuff


■ Create a directory tree in /tmp using one command with three subdirectories called one, two
and three:

$ mkdir -p /tmp/one/two/three

1.13 Removing Directories with rmdir command


■ rmdir command deletes empty directories, so the files inside must be deleted first

■ For example, to delete the images directory:


$ rm images/*
$ rmdir images
■ For non-empty directories, use rm -r directory
■ The -p option to rmdir removes the complete path, if there are no other files and directories in
it

● These commands are equivalent:


$ rmdir -p a/b/c
$ rmdir a/b/c a/b a

1.14 Identifying Types of Files

■ The data in files comes in various different formats (executable programs, text files, etc.)

■ The file command will try to identify the type of a file:


$ file /bin/bash
/bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1,
dynamically linked (uses shared libs), stripped
■ It also provides extra information about some types of file

■ Useful to find out whether a program is actually a script:


$ file /usr/bin/zless
/usr/bin/zless: Bourne shell script text
■ If file doesn’t know about a specific format, it will guess:
$ file /etc/passwd
/etc/passwd: ASCII text

5
1.15 Changing Timestamps with touch command
■ Changes the access and modification times of files

■ Creates files that didn’t already exist


■ Options:
● -a, change only the access time
● -m, change only the modification time
● -t [YYYY]MMDDhh [.ss ], set the timestamp of the file to the specified date and time
● GNU touch has a -d option, which accepts times in a more flexible format
■ For example, change the time stamp on homework to January 20 2001, 5:59p.m.
$ touch -t 200101201759 homework

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?

e. Try creating the dog/cat/mouse directory structure with a single command.

2. a. Copy the file /etc/passwd to your home directory, and then use cat to see what’s in it.

b. Rename it to users using the mv command.

c. Make a directory called programs and copy everything from /bin into it.

d. Delete all the files in the programs directory.

e. Delete the empty programs directory and the users file.

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?

d. Try setting the timestamp on the file to a value in the future.


e. When you’re finished with it, delete the file.

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

■ Either a command interpreter or a graphical user interface


■ Traditional Unix shells are command-line interfaces (CLIs)
■ Usually started automatically when you log in or open a terminal

User

Shell

Kernel

2.2 The Bash Shell


■ Linux’s most popular command interpreter is called bash
● The Bourne-Again Shell
● More sophisticated than the original sh by Steve Bourne
● Can be run as sh, as a replacement for the original Unix shell
■ Gives you a prompt and waits for a command to be entered
■ Although this course concentrates on Bash, the shell tcsh is also popular
● Based on the design of the older C Shell (csh)

2.3 Shell Commands


■ Shell commands entered consist of words
● Separated by spaces (whitespace)
● The first word is the command to run
● Subsequent words are options or arguments to the command ■
For several reasons, some commands are built into the shell itself

● Called builtins
● Only a small number of commands are builtins, most are separate programs

2.4 Command-Line Arguments


■ The words after the command name are passed to a command as a list of arguments

■ Most commands group these words into two categories:


● Options, usually starting with one or two hyphens
● Filenames, directories, etc., on which to operate
■ The options usually come first, but for most commands they do not need to
7
■ There is a special option ‘--’ which indicates the end of the options
● Nothing after the double hyphen is treated as an option, even if it starts with -

2.5 Syntax of Command-Line Options

■ Most Unix commands have a consistent syntax for options:


● Single letter options start with a hyphen, e.g., -B
● Less cryptic options are whole words or phrases, and start with two hyphens, for example
--ignore-backups
■ Some options themselves take arguments
● Usually the argument is the next word: sort -o
output_file

■ 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

2.7 Setting Shell Variables

■ Shell variables can be used to store temporary values

■ Set a shell variable’s value as follows:


$ files="notes.txt report.txt"
● The double quotes are needed because the value contains a space

● Easiest to put them in all the time


■ Print out the value of a shell variable with the echo command:
$ echo $files
● The dollar ($) tells the shell to insert the variable’s value into the command line
■ Use the set command (with no arguments) to list all the shell variables

8
9
2.8 Environment Variables

■ Shell variables are private to the shell


■ A special type of shell variables called environment variables are passed to
programs run from the shell
■ A program’s environment is the set of environment variables it can
access

● In Bash, use export to export a shell variable into the environment:


$ files="notes.txt report.txt"
$ export files
● Or combine those into one line:
$ export files="notes.txt
report.txt"
■ The env command lists environment variables

2.9 Where Programs are Found

■ The location of a program can be specified explicitly:


● ./sample runs the sample program in the current directory
● /bin/ls runs the ls command in the /bin directory
■ Otherwise, the shell looks in standard places for the program
● The variable called PATH lists the directories to search in
● Directory names are separated by colon, for example:
$ echo $PATH
/bin:/usr/bin:/usr/local/bin
● So running whoami will run /bin/whoami or /usr/bin/whoami or /usr/local/bin/whoami
(whichever is found first)

2.10 Bash Configuration Variables

■ Some variables contain information which Bash itself uses


● The variable called PS1 (Prompt String 1) specifies how to display the shell prompt

■ 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.,

● $USER, $HOSTNAME, $PWD

2.11 Using History


■ Previously executed commands can be edited with the Up or Ctrl+P keys
■ This allows old commands to be executed again without re-entering
■ Bash stores a history of old commands in memory
● Use the built-in command history to display the lines remembered
● History is stored between sessions in the file ˜/.bash_history
10
■ Bash uses the readline library to read input from the user
● Allows Emacs-like editing of the command line
● Left and Right cursor keys and Delete work as expected

2.12 Reusing History Items


■ Previous commands can be used to build new commands, using history expansion

■ Use !! to refer to the previous command, for example:


$ rm index.html
$ echo !!
echo rm index.html
rm index.html
■ More often useful is !string , which inserts the most recent command which started with
string
● Useful for repeating particular commands without modification:
$ ls *.txt
notes.txt report.txt
$ !ls
ls *.txt
notes.txt report.txt

2.13 Retrieving Arguments from the History

■ 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’)

2.15 Combining Commands on One Line

■ You can write multiple commands on one line by separating them with ;

■ Useful when the first command might take a long time:


time-consuming-program; ls
■ Alternatively, use && to arrange for subsequent commands to run only if earlier ones succeeded:
time-consuming-potentially-failing-program && ls

2.16 Repeating Commands with for


■ Commands can be repeated several times using for
● Structure: for varname in list ; do commands... ; done
■ For example, to rename all .txt files to .txt.old :
$ for file in *.txt;
> do
> mv -v $file $file.old;
> done
barbie.txt -> barbie.txt.old
food.txt -> food.txt.old
quirks.txt -> quirks.txt.old
■ The command above could also be written on a single line

2.17 Command Substitution


■ Command substitution allows the output of one command to be used as arguments to
another

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

2.18 Finding Files with locate

12
■ The locate command is a simple and fast way to find files

■ For example, to find files relating to the email program mutt:


$ locate mutt
■ The locate command searches a database of filenames
● The database needs to be updated regularly
● Usually this is done automatically with cron
● But locate will not find files created since the last update

■The -i option makes the search case-insensitive

■ -r treats the pattern as a regular expression, rather than a simple string

2.19 Finding Files More Flexibly: find


■ locate only finds files by name
■ find can find files by any combination of a wide number of criteria, including name
■ Structure: find directories criteria
■ Simplest possible example: find .
■ Finding files with a simple criterion:
$ find -name manual.html
Looks for files under the current directory whose name is manual.html

■ The criteria always begin with a single hyphen, even though


they have long names

2.20 find Criteria


■ find accepts many different criteria; two of the most useful are:
● -name pattern : selects files whose name matches the shell-style wildcard pattern
● -type d, -type f: select directories or plain files, respectively
■ You can have complex selections involving ‘and’, ‘or’, and ‘not’

2.21 find Actions: Executing Programs


■ find lets you specify an action for each file found; the default action is simply to print out the
name

● You can alternatively write that explicitly as -print


■ Other actions include executing a program; for example, to delete all files whose name starts
with manual:

find . -name ’manual*’ -exec rm ’{}’ ’;’


■ The command rm ’{}’ is run for each file, with ’{}’ replaced by the filename
■ The {} and ; are required by find, but must be quoted to protect
13
them from the shell

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.

c. Find out what the find criterion -iname does.

14
15

You might also like