|
|
|
|
|
|
|
|
|
|
|
Inspecting file content
|
|
|
UNIX provides a number of commands for working with files. The more common ones are described in this section. Note that these commands usually have several options and accept wildcard characters as arguments. For details, see the respective man pages which are hyperlinked to each command name. |
|
|
|
|
|
ls - lists files
|
|
|
|
|
|
Command
|
Description
|
ls
|
show contents of working directory
|
ls file
|
list file, if it exists in working directory
|
ls dir
|
show contents of the directory dir
|
ls -a
|
shows all your files, including hidden ones
|
ls -al
|
give detailed listing of contents
|
ls -F
|
mark directories with "/" and executable files with "*"
|
ls *.doc
|
show all files with suffix ".doc"
|
|
|
|
|
|
|
more
|
|
|
browses/displays files one screen at a time. Use h for help, spacebar to page, b for back, q to quit, /string to search for string |
|
|
|
|
|
more sample.f |
|
|
|
|
|
pg
|
|
|
browses/displays files one screen at a time. Similar to the more utility in function but has different commands and options. See the man page for details. |
|
|
|
|
|
pg sample.f |
|
|
|
|
|
less
|
|
|
similar to more, but with more features. Not available on every system. |
|
|
|
|
|
less sample.f |
|
|
|
|
|
head
|
|
|
displays the first n lines of a file |
|
|
|
|
|
Command
|
Description
|
head sample.f
|
display first 10 lines (default)
|
head -5 sample.f
|
display first 5 lines
|
head -25 sample.f
|
display first 25 lines
|
|
|
|
|
|
|
tail
|
|
|
displays the last n lines or n characters of a file |
|
|
|
|
|
|
|
|
Command
|
Description
|
tail sample.f
|
display last 10 lines (default)
|
tail -5 sample.f
|
display last 5 lines
|
tail -5c sample.f
|
display last 5 characters
|
tail -25 sample.f
|
display last 25 lines
|
|
|
|
|
|
|
cat
|
|
|
dumps the entire file to the screen without paging. This command is more useful for concatenating (hence the name "cat") files together than it is for reading files. |
|
|
|
|
|
Command
|
Description
|
cat myprog.c
|
diplays entire file
|
cat -b myprog.c
|
shows line numbers
|
cat file1 file2 > file3
|
adds file1 and file2 to make file3
|
|
|
|
|
|
|
cp
|
|
|
copies files. Will overwrite unless otherwise specified. Must also have write permission in the destination directory. |
|
|
|
|
|
Command
|
Description
|
cp sample.f sample2.f
|
copies sample.f to sample2.f
|
cp -R dir1 dir2
|
copies contents of directory dir1 to dir2
|
cp -i file.1 file.new
|
prompts if file.new will be overwritten
|
cp *.txt chapt1
|
copies all files with .txt suffix to directory chapt1
|
cp /usr/doc/README ~
|
copies file to your home directory
|
cp ~betty/index .
|
copies the file "index" from user betty's home directory to current directory
|
|
|
|
|
|
|
mv
|
|
|
moves files. Will overwrite unless otherwise specified. Must also have write permission in the destination directory. |
|
|
|
|
|
Command
|
Description
|
mv sample.f sample2.f
|
moves sample.f to sample2.f
|
mv dir1 newdir/dir2
|
moves contents of directory dir1 to newdir/dir2
|
mv -i file.1 file.new
|
prompts if file.new will be overwritten
|
mv *.txt chapt1
|
moves all files with .txt suffix to directory chapt1
|
|
|
|
|
|
|
|
|
|
|
|
|
rm
|
|
|
deletes/removes files or directories if file permissions permit. |
|
|
|
|
|
Command
|
Description
|
rm sample.f
|
deletes sample.f
|
rm chap?.txt
|
deletes all files with chap as the first four characters of their name and with .txt as the last four characters of their name
|
rm -i *
|
deletes all files in current directory but asks first for each file
|
rm -r /olddir
|
recursively removes all files in the directory olddir, including the directory itself
|
|
|
|
|
|
|
file filename
|
|
|
Report the file type |
|
|
|
|
|
For example: |
|
|
|
|
|
Command
|
Description
|
file *
|
reports all files in current directory and their types. The output might appear as shown below
|
about.html
|
ascii text
|
bin
|
directory
|
asic.directory
|
English text
|
bggen
|
executable or object module not stripped
|
bmbinc
|
commands text
|
machines.sp1
|
[nt]roff, tbl, or eqn input text
|
man2html
|
executable or object module not stripped
|
man2html.c
|
ascii text
|
|
|
|
|
|
|
find
|
|
|
finds files. The syntax of this command is: |
|
|
|
|
|
find pathname -name filename -print |
|
|
|
|
|
- The pathname defines the directory to start from. Each subdirectory of this directory will be searched.
- The -print option must be used to display results.
- You can define the filename using wildcards. If these are used, the filename must be placed in 'quotes'.
|
|
|
|
|
|
Command
|
Description
|
find . -name mtg_jan92 -print
|
looks for the file mtg_jan92 in current directory
|
find ~/ -name README -print
|
looks for files called README throughout your home directory
|
find . -name '*.fm' -print
|
looks for all files with .fm suffix in current directory
|
find /usr/local -name gnu -type d -print
|
looks for a directory called gnu within the /usr/local directory
|
|
|
|
|
|
|
diff
|
|
|
comparing two files or directories. Indicates which lines need be added (a), deleted (d) or changed (c). Lines in file1 are identified with a (<) symbol: lines in file2 with a (>) symbol |
|
|
|
|
|
Command
|
Description
|
diff file1 file2
|
compares file1 to file2
|
diff -iw file1 file2
|
compares two files ignoring letter case and spaces
|
diff dir1 dir2
|
compares two directories showing files which are unique to each and also, line by line differences between any files in common.
|
|
|
|
|
|
|
For example, if file1 and file2 are: |
|
|
|
|
|
File1
|
File2
|
John erpl08@ed
|
John erpl08@ed
|
Joe CZT@cern.ch
|
Joe CZT@cern.ch
|
Kim ks@x.co
|
Jean JRS@pollux.ucs.co
|
Keith keith@festival
|
Jim jim@frolix8
|
|
Kim ks@x.co
|
|
Keith keith@festival
|
|
|
|
|
|
|
Using the diff command: diff file1 file2 Yields the output: |
|
|
|
|
|
2a3,4 |
|
|
> Jean JRS@pollux.ucs.co |
|
|
> Jim jim@frolix8 |
|
|
|
|
|
Which means that to make these files match you need to add (a) lines 3 and 4 (3,4) of file2 (>) after line 2 in file1. |
|
|
|
|
|
sdiff
|
|
|
similar to diff, but displays each line of the two files side by side, making it easier for you to see the differences between them |
|
|
|
|
|
Lines that are different are shown with a | symbol. Lines unique to file1 are identified by a < symbol; lines unique to file2 with a > symbol. Identical lines appear next to each other. The option -w 80 is used to set the width of the output from the command to 80 characters. The default is 130 characters. |
|
|
|
|
|
File1
|
File2
|
sdiff -w 80 file1 file2
|
|
Mike erpl08@ed
|
| John erpl08@ed
|
Joe CZT@cern.ch
|
Joe CZT@cern.ch
|
|
> Jean JRS@pollux.ucs.co
|
|
> Jim jim@frolix8
|
Kim ks@x.co
|
Kim ks@x.co
|
Sam s.wally@aston
|
<
|
Keith keith@festival
|
Keith keith@festival
|
|
|
|
|
|
|
ln
|
|
|
Direct (hard) and indirect (soft or symbolic) links from one file or directory to another can be created using the ln command. |
|
|
|
|
|
$ ln filename linkname |
|
|
|
|
|
creates another directory entry for filename called linkname (i.e. linkname is a hard link). Both directory entries appear identical (and both now have a link count of 2). If either filename or linkname is modified, the change will be reflected in the other file (since they are in fact just two different directory entries pointing to the same file). |
|
|
|
|
|
$ ln -s filename linkname |
|
|
|
|
|
creates a shortcut called linkname (i.e. linkname is a soft link). The shortcut appears as an entry with a special type ('l'): |
|
|
|
|
|
$ ln -s hello.txt bye.txt |
|
|
$ ls -l bye.txt |
|
|
lrwxrwxrwx 1 will finance 13 bye.txt -> hello.txt |
|
|
$ |
|
|
|
|
|
The link count of the source file remains unaffected. Notice that the permission bits on a symbolic link are not used (always appearing as rwxrwxrwx). Instead the permissions on the link are determined by the permissions on the target (hello.txt in this case). |
|
|
|
|
|
Note that you can create a symbolic link to a file that doesn't exist, but not a hard link. Another difference between the two is that you can create symbolic links across different physical disk devices or partitions, but hard links are restricted to the same disk partition. Finally, most current UNIX implementations do not allow hard links to point to directories. |
|
|
|
|
|
sort
|
|
|
sorts files, merges files that are already sorted, and checks files to determine if they have been sorted. The command syntax is: |
|
|
|
|
|
sort options filename |
|
|
|
|
|
- By default, lines in "filename" are sorted and displayed to the screen. If the "filename" parameter specifies more than one file, the sort command concatenates the files and sorts them as one file.
- An output file can be specified with the -o flag.
- Files can be sorted by "fields" - single or multiple.
- The sort command supports many options. See the man page for details.
|
|
|
|
|
|
Command
|
Description
|
sort addresses
|
sorts the file addresses and displays output on screen
|
sort -o sorted addresses
|
sorts the file addresses and writes output to the file called sorted.
|
sort -u -o mail_labels addresses
|
removes all duplicate lines from the file addresses and writes the output in the file mail_labels.
|
sort +2 -4 addresses
|
sorts the file by its third and fourth fields. Note that +2 means to skip first two fields and -4 means to stop after the fourth field.
|
|
|
|
|
|
|
pwd
|
|
|
print working directory. Tells you which directory you are currently in. |
|
|
|
|
|
pwd |
|
|
|
|
|
|
|
|
mkdir
|
|
|
Make directory. Will create the new directory in your working directory by default. |
|
|
|
|
|
mkdir /u/training/data |
|
|
mkdir data2 |
|
|
|
|
|
cd
|
|
|
Change to specified directory. May specify either the absolute or relative pathname. cd with no pathname changes to your home directory. |
|
|
|
|
|
Command
|
Description
|
cd /usr/local
|
change to /usr/local
|
cd doc/training
|
change to doc/training in current directory
|
cd ..
|
change to parent directory
|
cd ~/data
|
change to data directory in home directory
|
cd ~joe
|
change to use joe's home directory
|
cd
|
change to home directory
|
|
|
|
|
|
|
rmdir
|
|
|
remove directory. Directories must be empty before you remove them. |
|
|
|
|
|
rmdir project1 |
|
|
|
|
|
To recursively remove nested directories, use the rm command with the -r option: |
|
|
|
|
|
rm -r dirctory_name |
|
|
|
|
|
Command summary
|
|
|
A summary of commands and utilities related to the UNIX file system appears below. See the corresponding man pages for detailed information. |
|
|
|
|
|
Command
|
Description
|
awk
|
search for and process patterns in a file,
|
cat
|
display, or join, files
|
cd
|
change working directory
|
chgrp
|
change the group that is associated with a file
|
chmod
|
change the access mode of a file
|
chown
|
change the owner of a file
|
comm
|
compare sorted files
|
cp
|
copy files
|
df
|
display the amount of available disk space
|
diff
|
display the differences between two files
|
du
|
display information on disk usage
|
file
|
display file classification
|
find
|
find files
|
fsck
|
check and repair a file system
|
grep
|
search for a pattern in files
|
head
|
display the first few lines of a file
|
ln
|
create a link to a file
|
lp
|
print files (System V)
|
lpr
|
print files (Berkeley)
|
ls
|
list information about files
|
mkdir
|
create a directory
|
more
|
display a file one screen at a time (System V)
|
mv
|
move and/or rename a file
|
od
|
dump a file
|
pg
|
display a file one screen at a time (Berkeley)
|
pr
|
paginate a file
|
pwd
|
print the working directory
|
rm
|
remove (delete) files
|
rmdir
|
remove (delete) a directory
|
sed
|
stream editor (non-interactive)
|
sort
|
sort and/or merge files
|
spell
|
check a file for spelling errors
|
tail
|
display the last few lines of a file
|
tar
|
store or retrieve files from an archive file
|
umask
|
set file creation permissions
|
uniq
|
display the lines in a file that are unique
|
wc
|
counts lines, words and characters in a file
|
whatis
|
list man page entries for a command
|
whereis
|
show where executable is located in path
|
which
|
locate an executable program using "path"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|