Linux File Management
From Colwiki.org
Upon completion of this module you will be able to:
|
|
Contents |
Moving around the File System
Absolute and relative paths
A directory or a file can be accessed by giving its full pathname, starting at the root (/) or its relative path, starting from the current directory.
Absolute path: independent of the user's current directory starts with / Relative path: depends on where the user is doesn't start with /
As in any structured file system there are a number of utilities that can help you navigate through the system. The next two commands are built-in commands. pwd: Gives your actual position as an absolute path. cd: The 'change directory' command
Finding Files and Directories
We will describe the find, which, whereis and locate utilities.
find
Syntax: find <DIRECTORY> <CRITERIA> [-exec <COMMAND> {} \;]
The DIRECTORY argument tells find where to start searching and CRITERIA can be the name of a file or directory we are looking for.
Examples: find /usr/X11R6/bin -name ¨x*¨. find / -user 502
Matching lines are listed to standard out. This output can be acted upon. For example delete the file, or change the permission. The find tool has the build-in option –exec which allows you to do that. For example, remove all files belonging to user 502:
find / -type f -user 502 –exec rm –f {} \;
xargs
This tool is often thought of as a companion tool to find. In fact xargs will process each line of standard output as an argument for another tool. We could use xargs to delete all files belonging to a user with:
find / -type f -user 502 | xargs rm –f
Certain commands such as rm cannot deal with too long arguments. It is sometimes necessary to delete all files in a directory with
ls |xargs rm -f
Common criteria switches for find -type specify the type of file -name name of the file -user user owner -atime, ctime, mtime access, creation and modified times (multiples of 24 hrs) -amin, cmin, mmin access, creation and modified times (multiples of 1 min) -newer FILE files newer than FILE
locate
Syntax:
locate <STRING>
When using locate all files and directories that match the expression are listed.
locate X11R
The search is much faster. In fact locate queries the /var/lib/slocate database. This database is kept up to date via a daily cron job which runs updatedb. When running updatedb from the command line the /etc/updatedb.conf file is read to determine pruned files systems (e.g NFS) and directories (e.g /tmp)
which
Syntax:
which string
This tool will return the full path to the file called string by scanning the directories defined in the user's PATH variable only. As a result which is only used to find commands.
whereis
Syntax
whereis string
This tool will return the full path to source or binaries as well as documentation files matching string by scanning the PATH variable as well as a number of well known locations
Getting the most from ls
Most common options for ls -I show inode -h print human readable sizes -n list UIDs and GIDs -p append descriptor (/=@) to list -R recursively display content of directories -S sort by file size -t sort by modification time (similar to -c) -u show last access time
Handling Directories
Making a directory with mkdir:
When making a directory you can set the permission mode with the –m option. Another useful option is -p which creates all subdirectories automatically as needed.
Example: mkdir –p docs/programs/versions
Removing directories:
To remove a directory use either rmdir or rm -r. If you are root you may have to specify -f to force the deletion of all files.
Notice: rm –rf /dir1/* removes all files and subdirectories leaving dir1 empty rm –rf /dir1/ removes all files and subdirectories including dir1
Using cp and mv
cp
Syntax: cp [options] file1 file2 cp [options] files directory
It is important to notice that cp file1 file2 makes a new copy of file1 and leaves file1 unchanged. The figure below shows file1 with inode 250 is copied to file2, duplicating the data to a new data area and creating a new inode 6238 for file2
You can also copy several files to a directory, using a list or wildcards. The following table lists the most used options.
Most common options for cp -d do not follow symbolic link (when used with -R) -f force -I interactive, prompt before overwrite -p preserve file attributes -R recursively copy directories Note: cp –r /dir/* /dir2/ will copy all files and subdirectories omitting mydir cp –r /mydir/ /dir2/ will copy all files and subdirectories including mydir
mv Syntax: mv [options] oldname newname mv [options] source destination mv [options] source directory
The mv command can both move and rename files and directories. If oldname is a file and newname is a directory then the file oldname is moved to that directory. If the source and destination are on the same filesystem, then the file isn't copied but the inode information is updated to specify the new location. Most common options are -f forces overwrite and -i query interactively.
Hard and Symbolic Links
Symbolic links
A soft link to a file or a directory creates a new inode that points to the same data area:
ln -s lilo.conf lilo.sym
This is the listing for these files. Notice that the reference count is 1 for both files.
-rw------- 1 root root 223 Nov 9 09:06 lilo.conf lrwxrwxrwx 1 root root 9 Nov 9 09:06 lilo.sym -> lilo.conf
A soft link to a file
Soft links can be created across filesystems.
Hard Links
A hard link is an additional name for the same inode and as such the reference count of the file increases by one for every new hard link.
ln lilo.conf lilo.link'
In the listing notice that the reference count is 2 and that both files have the same size. In fact they are identical.
-rw------- 2 root root 223 Nov 9 09:06 lilo.conf -rw------- 2 root root 223 Nov 9 09:06 lilo.link
Hard links can only be created within the same filesystem.
Touching and dd-ing
Touch
Another way of creating or modifying a file is to use touch.
Syntax: touch {options} file(s)
If file doesn't exist it is created. You can also change the access time of a file using the -a option, -m changes the modification time and -r is used to apply the time attributes of another file.
Example: touch file1.txt file2.txt creates new files touch myfile -r /etc/lilo.conf myfile gets the time attributes of lilo.conf
To create a file called –errors use the – option: touch -- -errors
dd
This command copies a file with a changeable I/O block size. It can also be used to perform conversions (similar to tr). Main options are if= (input file) of= (output file) conv= (conversion) The conversion switch can be: lcase ucase ascii
Example: dd if=/mnt/cdrom/images/boot.img of=/dev
| In this lesson you have learned how to navigate around the Linux file system. The commands you have learned in this module are critical for your effective operations within the Linux system. You notice that you were able to change the operations of a linux command by using appropriate switches/options. This is common across all the Linux commands as this is a design principle common in all linux applications. |
|
Make a new directory in /tmp called /bin. mkdir /tmp/bin In /tmp/bin/ create a file called newfile (use touch, cat or vi). Go to the root directory (cd /). View the content of newfile from there. Which is the shortest command which will take you back to /tmp/bin ? Which is the shortest command which will take you to your home directory ? Is the PWD variable local or global ? Creating and deleting directories Which is the quickest way to make two new directories /dir1/dir2 ? Remove the directories with rmdir then with rm. Making space on the filesystem In order to create more space on the device containing the directory /usr/share/doc we need to find a spare device with enough space and copy the contents of /usr/share/doc to that device. Then we create create by deleting the /usr/share/doc directory and creating a symbolic link point from /usr/share/doc to the new location. Make a directory called /spare on which we will mount suitable spare devices (one of the partitions created in the previous exercises should be suitable. mkdir /spare mount <device> /spare Test with df -h /spare and du -hs /usr/share/doc that the device is large enough to contain all of the existing data. Next, copy the contents of /usr/share/doc to /spare/ cp -a /usr/share/doc /spare Make sure the data has all been copied across then edit /etc/fstab to make that device available at boot time. Delete /usr/share/doc and create a symbolic link pointing from /usr/share/doc to /spare/doc ln -s /spare/doc /usr/share/doc Do the same with /home. Any extra problems?
Finding Files on the System Copy the file /etc/lilo.conf to /etc/lilo.conf.bak
Find all files in your home directory that have been modified today. find /home –mtime –1 |
This work is licenced under a Creative Commons - By Attribution Licence - Share Alike License.



