Slide 02 2 File and Directory Commands
Slide 02 2 File and Directory Commands
Source(Ubuntu)
$ cd path
✓ One of the most common relative paths to use is '..' (i.e. the parent directory of the
current directory).
$ mkdir directory
rmdir (remove directory) rmdir cannot remove directories that have files or
directories in them. To do that, you must use the rm
$ rmdir directory command rm -rf [directory name]
✓ removes the subdirectory directory from the current working directory. You can only
remove subdirectories if they are completely empty (i.e. of all entries besides the '.'
and '..' directories).
cp (copy)
$ cp source-file(s) destination
✓ where source-file(s) and destination specify the source and destination of the copy
respectively
Directory and File Handling Commands
cat (catenate/type)
$ cat target-file(s)
✓ displays the contents of target-file(s) on the screen, one after the other.
✓ You can also use it to create files from keyboard input as follows (> is the output
redirection operator:
$ cat > hello.txt
hello world!
[ctrl-d]
$ ls hello.txt
hello.txt
$ cat hello.txt
hello world!
$
Directory and File Handling Commands
✓ 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 financ e13 bye.txt -> hello.txt
$
Inspecting File Content
file filename(s)
✓ file analyzes a file's contents for you and reports a high-level description of
what type of file it appears to be:
$ file myprog.c letter.txt webpage.html
myprog.c: C program text
letter.txt: English text
webpage.html: HTML document text
head, tail filename
✓ head and tail display the first and last few lines in a file respectively.
✓ You can specify the number of lines as an option, e.g.
$ tail -20 messages.txt
$ head -5 messages.txt
Inspecting File Content
✓ tail includes a useful -f option that can be used to continuously monitor the last
few lines of a (possibly changing) file.
✓ This can be used to monitor log files, for example:
$ tail -f /var/log/messages
✓ continuously outputs the latest additions to the system log file.
Finding Files
find
$ find directory -name targetfile -print
✓ find will look for a file called targetfile in any part of the directory tree rooted at
directory. targetfile can include wildcard characters. For example:
$ find /home -name "*.txt" -print 2>/dev/null
✓ will search all user directories for any file ending in ".txt" and output any
matching files (with a full absolute or relative path).
✓ Here the quotes (") are necessary to avoid filename expansion,
Finding Files
find
✓ It can find files
➢ by type (e.g. -type f for files, -type d for directories),
➢ by permissions (e.g. -perm o=r for all files and directories that can be
read by others),
➢ by size (-size) etc.
✓ For more information about find and its abilities, use man find and/or info
find.
Finding Files
find
✓ The "find" command in Ubuntu is a powerful tool used to search for files and
directories based on various criteria.
✓ It allows you to locate files by their names, types, sizes, permissions, and more.
Here's how you can use the "find" command:
find
✓ Searching by size: The "-size" option allows you to search for files based on their size.
You can specify the size using different units such as "k" (kilobytes), "M" (megabytes),
or "G" (gigabytes). For example, to find files larger than 10MB:
find / -size +10M
✓ Combining search criteria: You can combine multiple search criteria using logical
operators like "and" (-a), "or" (-o), and "not" (!). For example, to find all files with a
".txt" extension in the "/home" directory and its subdirectories:
find /home -name "*.txt"
✓ These are just a few examples of how you can use the "find" command in Ubuntu. It
provides many more options and possibilities for searching files and directories. Feel
free to explore the manual page for more details by running the command man find.
Finding Files
Whereis
✓ The whereis command will display the location of a command executable, as well as
its source and documentation, if available.
Finding Text in files
✓ If you want to search all files in an entire directory tree for a particular pattern, you can
combine grep with find using backward single quotes to pass the output from
find into grep.
✓ So
$ grep hello `find . -name "*.txt" -print`
✓ will search all text files in the directory tree rooted at the current directory for lines
containing the word "hello".
Sorting files
sort filenames
✓ sort sorts lines contained in a group of files alphabetically numerically.
✓ The sorted output is displayed on the screen, and may be stored in another file by
redirecting the output. So
$ sort input1.txt input2.txt > output.txt
✓ outputs the sorted concatenation of files input1.txt and input2.txt to the
file output.txt.
File Compression and Backup
Using 'gzip'
✓ 'tar' just assembles the files together into only one
file. There is no reduction in the size of these files
✓ Now we would have to do one more thing in order to
reduce this file into a more manageable size: use
'gzip'.
✓ gzip is the preferred compression tool for Linux.
✓ To reduce the size of your tar file, you would issue the
following command:
$ gzip your_tar_file.tar
✓ The result would be a file like this:
your_tar_file.tar.gz
Redirecting input and output
✓ The output from programs is usually written to the screen, while their input usually
comes from the + (if no file arguments are given).
✓ In technical terms, we say that processes usually write to standard output (the screen)
and take their input from standard input (the keyboard).
✓ There is in fact another output channel called standard error, where processes write
their error messages; by default error messages are also sent to the screen.
✓ To redirect standard output to a file instead of the screen, we use the > operator:
$ echo hello
hello
$ echo hello > output
$ cat output
hello
✓ In this case, the contents of the file output will be destroyed if the file already
exists.
Redirecting input and output
✓ If instead we want to append the output of the echo command to the file, we can use
the >> operator:
$ echo bye >> output
$ cat output
hello
bye
✓ To capture standard error, prefix the > operator with a 2 (in UNIX the file
numbers 0, 1 and 2 are assigned to standard input, standard output and standard error
respectively), e.g.:
$ cat nonexistent 2>errors
$ cat errors
cat: nonexistent: No such file or directory
$
Redirecting input and output
✓ You can redirect standard error and standard output to two different files:
$ find . -print 1>outputfile 2>errorfiles
✓ or to the same file:
$ find . -print 1>output 2>output
✓ Standard input can also be redirected using the < operator, so that input is read
from a file instead of the keyboard:
$ cat < existing_file
hello
Bye
File and Directory Permissions
✓ In this section we'll learn about how to set Linux permissions on files and directories.
✓ Permissions specify what a particular person may or may not do with respect to a file
or directory.
✓ Linux permissions dictate 3 things you may do with a file, read, write and execute.
✓ They are referred to in Linux by a single letter each.
➢ r read - you may view the contents of the file.
➢ w write - you may change the contents of the file.
➢ x execute - you may execute or run the file if it is a program or script.
✓ For every file we define 3 sets of people for whom we may specify permissions.
➢ owner - a single person who owns the file. (typically the person who created
the file but ownership may be granted to some one else by certain users)
➢ group - every file belongs to a single group.
➢ others - everyone else who is not in the group or the owner.
File and Directory Permissions
View Permissions
✓ To view permissions for a file we use the long listing option for the command ls.
Ls –l [path]
g24@Ubuntu:~$ ls -l /home/ryan/linuxtutorialwork/frog.png
✓ In the above example the first 10 characters of the output are what we look at to identify
permissions.
➢ The first character identifies the file type. If it is a dash ( - ) then it is a normal
file. If it is a d then it is a directory.
File and Directory Permissions
➢ The following 3 characters represent the permissions for the owner. A letter represents
the presence of a permission and a dash ( - ) represents the absence of a permission.
In this example the owner has all permissions (read, write and execute).
➢ The following 3 characters represent the permissions for the group. In this example the
group has the ability to read but not write or execute.
Note that the order of permissions is always read, then write then execute.
➢ Finally the last 3 characters represent the permissions for others (or everyone else). In
this example they have the execute permission and nothing else.
File and Directory Permissions
Change Permissions
➢ Who are we changing the permission for? [ugoa] - user (or owner), group,
others, all
✓ Grant the execute permission to the group. Then remove the write permission for the
owner
File and Directory Permissions
✓ To understand how this shorthand method works we first need a little background in
number systems…(Binary, Octal ,Decimal, Hexadecimal etc..)
✓ Now it just so happens that with 3 permissions and each being on or off, we have 8
possible combinations (2^3). And then we can also represent our numbers using
binary which only has 2 symbols (0 and 1).
✓ Let's see some examples. (refer to the table above to see how they match)
✓ The same series of permissions may be used for directories but they have a slightly
different behaviour.
➢ r - you have the ability to read the contents of the directory (ie do an ls).
➢ w - you have the ability to write into the directory (ie create files and
directories)