File Commands
Notes:
Some file commands:
-
more - display file contents one screen at a time. Keystroke commands
for more:
-
[SpaceBar] - display the next screen
-
[Enter] - advance 1 line
-
b - go backwards
-
/xyz - search for the string xyz
-
q - quit
-
less - a more powerful version of more
-
head - display lines from the beginning of a file The default is 10
lines.
-
head -15 /etc/passwd
displays the first 15 lines of /etc/passwd
-
tail - display lines from the end of a file
-
cp - copies a file or files
-
cp oldfile newfile
copies oldfile; the copy will be called newfile
-
cp oldfile newdir
(assuming newdir is a directory) copies oldfile; the copy will be called
oldfile and will be stored in the directory newdir
-
cp f* newdir
copies all files with a filename beginning with the letter f to the directory
newdir; if newdir is not a directory, this concatenates all files beginning
with the letter f into a single file called newdir
-
cp f? newdir
copies all files with two letter filenames where the first letter is f to
the directory newdir
-
cp b[aeiu]t newdir
copies the files bat, bet, bit and but to the directory newdir
-
mv - Moves instead of copies
-
the mv command has the same syntax as the cp command
-
the mv command can also be used to rename a file
-
rm - This command deletes a file. Examples:
-
rm file1
delete file1
-
rm *
delete all files in the working directory
-
rm -i *
prompt to confirm before deleting each file
-
touch - This command changes the access and modification time of a
file to the current time. If the file does not exist, touch will create an
empty file using that name.
-
ln - create a link to an existing file. Examples:
-
ln -s filename linkname
creates a link or a pointer. The link will be called linkname and it will
point to filename.
Exercises:
-
Load Floppix and login on virtual console 1 using your own initials.
-
Get a listing of the files in your own home directory.
-
What hidden files (filename begins with . ) are present in your own home
directory?
-
What hidden files are present in the directory /home/alterego?
-
Use head or tail to do the following:
-
Display the last 4 lines of the file /etc/passwd.
-
Display the last 4 lines of the file /etc/group.
-
Display the first 10 lines of the file /etc/login.defs.
-
Practice the copy command:
-
Copy files in your current directory:
Make sure that you are in your own home directory.
Make a copy of readme called readme2. Get a directory listing to make sure
that the copy command worked.
Make a copy of .bash_profile called bprofile. Get a directory listing to
make sure that the copy command worked.
-
Copy files from another directory:
Copy the file called penguin from /home/floopy to your own home directory.
Make sure that the copy command worked.
Copy the file called motd from the /etc directory to your own home directory.
Make sure that the copy command worked.
Copy the file called .plan in the directory /home/alterego to a file called
plan in your own home directory. Make sure that the copy command worked.
-
Copy files to another directory:
Copy the readme file to the /tmp directory. As part of the copy, rename the
file readme.abc. Get a listing of the tmp directory to make sure that the
copy worked.
-
In your own home directory, use the touch command to create five files: one,
two, three, four and five. Get a directory listing to make sure that the
command worked.
-
Practice making directories.
-
In your own home directory, make 2 subdirectories: dir1 and dir2. Get a directory
listing to make sure that the command worked.
-
In dir2, make a subdirectory called dir2a .
-
Practice moving files from your own home directory:
-
Move the file called one to the directory dir1 . Get directory listings of
the source directory and dir1 to make sure that the move command worked.
-
Use a single command and wildcard characters to move all files in your home
directory that begin with the letter "t" to dir2 . Get a directory listing
to make sure that the move command worked.
-
Use a single command and wildcard characters to move all files in your home
directory that begin with the letter "f" to the directory dir2a . Get a directory
listing to make sure that the move command worked.
-
Use the move command to rename files.
-
Use the move command to change the name of the file called readme2 to readme.new.
-
Create a subdirectory called aaa.
-
Can you use the move command to change the name of aaa to dir3? _____
Questions & Answers:
-
What is a "pager"?
Answer: A pager displays the contents of a file one screen or page
at a time. "more" and "less" are both pagers.
-
What command will display the last 5 lines of the file /etc/passwd?
Answer: tail -5 /etc/passwd
-
What command will copy the file /etc/passwd to your own home directory?
Answer: cp /etc/passwd ~
-
What command will copy all files that start with h from the /etc directory
to your own home directory?
Answer: cp /etc/h* ~
-
What command will create 3 files: r1, r2 and r3?
Answer: touch r1 r2 r3
-
What command will copy r1 and r2 to the /tmp directory?
Answer: cp r[12] /tmp
-
What command will delete the files r1, r2 and r3 but not readme?
Answer: rm r?
Copyright © L.
M. MacEwan