|
|
Disk Usage of a
Specific Directory
Linux Tips by Burleson Consulting |
As we create and remove
files and directories it is often important to keep track of disk usage. For
that we use the du (disk usage) command.
$ du -hs .
2.3M .
Here du is called with the
options h to show the output in a human readable format and s to show only the
summary of all disk usage in this directory. While we specified the working
directory (.) in this example any directory could be given here.
File Commands
Creating an Empty File
Sometimes, it is useful to create an empty file as a
placeholder for future content. The touch command can be used without any
options to create an empty file as follows:
$ touch /home/tclark/touch1.fil
$ touch touch2.fil
$ ls ?l
total 4
drwxrwxr-x 2 tclark tclark 4096 Jan 13 17:48 examples
-rw-rw-r-- 1 tclark tclark 0 Jan 13 19:13 touch1.fil
-rw-rw-r-- 1 tclark tclark 0 Jan 13 19:14 touch2.fil
The touch command also has
an option that allows the file timestamp information of an existing file to be
changed using the ?t option. In the following example, the timestamp on the
touch1.fil above will be changed to January 10 12:00. The date format for this
option is given as a string of numbers with two digits each for month, date,
hour (in 24 hour format) and minute, so the string for January 10 12:00 would be
01101200. For instance, the user may want to change a group of files in a
directory to indicate a particular release date.
$ touch -t 01101200
touch1.fil
$ ls -l
total 4
drwxrwxr-x 2 tclark tclark 4096 Jan 13 17:48 examples
-rw-rw-r-- 1 tclark tclark 0 Jan 10 12:00 touch1.fil
-rw-rw-r-- 1 tclark tclark 0 Jan 13 19:14 touch2.fil
A Brief Discussion about Wildcards
Many of the common file and
directory manipulation commands, including most of the ones mentioned in the
balance of this chapter, will let you use a partial file or path name and
substitute a wildcard character for the rest of one or several files. These
wildcards can save you a lot of typing, but can also leave you with ambiguous
results, possibly even causing the accidental deletion of files!
The two most common wildcard characters are the asterisk
(*) and the question mark (?). The *, commonly called the star, will match any
number of any characters within a filename, even if there are none. The ? will
match any single character within a filename.
Here are a couple examples which should make wildcards a
bit more clear:
$ ls
examples touch1.fil touch2.fil
$ ls tou*
touch1.fil touch2.fil
$ ls touch?.fil
touch1.fil touch2.fil
$ ls *ch?.fil
touch1.fil touch2.fil
In the last example we see
that multiple wildcards can be used within the same statement and both will be
applied to the results. The most important thing to remember about wildcards is
that they will not just match one but will instead match all files which have
the pattern you have described.
Deleting Files
The remove (rm) command can be used to delete a file;
referencing the file either via an absolute path or via a relative path.
Wildcard symbols such as * and ?, can be used to delete files with similar
names.
* Be careful with the rm command, especially as root!
Linux tends to just delete what you tell it no questions asked!
Here are some examples of the rm command:
$ rm /home/tclark/touch1.fil
$ ls
examples touch2.fil
$ rm touch2.fil
$ ls
examples
$ touch touch1.fil touch2.fil touch3.fil
$ ls
examples touch1.fil touch2.fil touch3.fil
$ rm touch?.fil
$ ls
examples
The rm command can be used with the ?r (recursive)
option to remove a directory and its contents. The rm ?r command, as with other
commands which affect multiple files, should be used with caution.
$ mkdir ?p
new_dir3/sub_dir3
$ ls
examples new_dir3
$ rmdir new_dir3
rmdir: `new_dir3': Directory not empty
$ rm -r new_dir3
$ ls
examples
Preventing Accidental
File Deletion
A little trick that some Linux users like to implement
to prevent accidental file deletion is via the use of an alias. Aliases are
similar to variables and can either be set in a session or by placing the
command alias in the .bash_profile file with a text editor.
By adding this alias the user will be prompted to
confirm each file before it is deleted; otherwise Linux, unlike Microsoft
Windows, will delete whatever files match the filename criteria without warning!
$ alias rm='rm -i'
$ touch touch1.fil touch2.fil touch3.fil
$ rm touch*
rm: remove regular empty file `touch1.fil'? y
rm: remove regular empty file `touch2.fil'? y
rm: remove regular empty file `touch3.fil'? y
Deleting Files Using
File Properties
When doing some file
cleanup operations and it becomes desirable to delete all files within a
directory that have not been used or accessed in more than 30 days, the
following command example will be useful:
$ find . ?maxdepth 1 -atime +30 -exec rm {} \;
This find command locates files which have not been
accessed in 30 days, and then executes a rm command for each file found. The
find command can be very useful for performing operations on multiple files. We
will discuss find in much more detail in chapter 4.
Moving and Renaming Files
The mv (move) command will allow you to move or rename
files and directories. Linux does not discern between these two functions as
renaming can be just considered moving a file to a different name.
The rules for mv are similar to those for rm described
earlier, in that file references can be via a fully qualified path or via a
relative path. The use of the wildcard symbols in the filename is also
allowed. The following are some examples:
* Move file example1.fil from the current working
directory to the /tmp directory:
$ mv example1.fil /tmp
* Move file example1.fil using the fully qualified path to the /tmp directory:
$ mv /home/tclark/example2.fil /tmp
* Rename temp1.fil in the /tmp directory to temp1.xxx from the home directory:
$ cd /tmp
$ touch temp1.fil temp2.fil
$ ls -l temp*
-rw-rw-r-- 1 tclark tclark 0 Jan 13 21:15 temp1.fil
-rw-rw-r-- 1 tclark tclark 0 Jan 13 21:15 temp2.fil
$ cd ?
/home/tclark
$ mv /tmp/temp1.fil /tmp/temp1.xxx
$ cd -
/tmp
$ ls temp*
temp1.xxx temp2.fil
This example demonstrates how by using absolute paths we
can perform a command on a directory which is not our working directory.
* Rename example2.fil in the current working directory
to example2.xxx:
$ ls
examples
$ touch example1.fil example2.fil
$ ls
example1.fil example2.fil examples
$ mv example2.fil example2.xxx
$ ls
example1.fil example2.xxx examples
So far we've discussed several ways to manipulate files
and directories. Since these commands are so powerful you may find that you
want to make a backup before you make certain changes. We'll look at how to do
this in the next section.
This is an excerpt from "Easy
Linux Commands" by Linux guru Jon Emmons. You can purchase it for only
$19.95 (30%-off) at
this link.