File management in Linux
Saturday, 8 September 2007, adz
Unix systems provide a number of commands to manage files and directories. Their strong point is the ability to use them in a rather simple manner against a group of files/directories meeting certain conditions. For example all the files satisfying specific criteria can be deleted or have their names changed en masse.
1. ls
It is very frequently used command. It redirects directory contents to standard output. If issued without any path it will list current directory (where we are at present).
adam@laptop:~/Documents/polishlinux.org$ pwd
/home/adam/Documents/polishlinux.org/
adam@laptop:~/Documents/polishlinux.org$ls
example.txt all_about_console.txt
adam@laptop:~/Documents/polishlinux.org$ls /var/
backups cache crash games lib local lock log mail opt run spool
tmp
Second example in the listing above shows ls invoked with “/var/” directory path. The ls command can reveal more detailed data if run with the “-l” parameter.
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
total 0
-rw-r--r-- 1 adam adam 0 2007-05-30 11:31 example.txt
-rw-r--r-- 1 adam adam 0 2007-05-30 11:31 all_about_console.txt
The first row of ls output shows total memory blocks taken by files from the directory. The next rows are ordered as follows:
- -rw-r–r– file and directory permissions (more about permissions in the next part of this guide),
- number of hard links to the file,
- file owner then a group the owner belongs to,
- file length,
- time of latest modification,
- file/directory name.
ls command can display hidden files (so called “dot files”) as well. Names of the hidden files begin with a dot .. To show the files we need a -a parameter.
adam@laptop:~/Documents/polishlinux.org/examples$ ls -a
. .. .hidden_file example.txt all_about_console.txt
We are able to sort files using the ls command. The following parameters are available:
- -t - displays contents sorted according to modification time (from latest to oldest),
- -S - displays contents according to size (from biggest to smallest),
- -r - reverses sort order.
There is also a possibility to list directories recursively, that is, to display contents of all subdirectories found in a given directory - it is served by -R parameter.
adam@laptop:~/Documents/polishlinux.org/examples$ ls -R
.:
directory1 directory2 example.txt all_about_console.txt
./directory1:
file1 file2
./directory2:
file3 file4
2. cd
Command change directory (cd) allows us to move through directory trees. It has one parameter - target directory path.
adam@laptop:~$cd /usr/bin
adam@laptop:/usr/bin$pwd
/usr/bin
It is worth noting the tilde character ~ here. It is one of the special characters recognized by all system shells. It denotes a path to the user’s home directory.
adam@laptop:/usr/bin$cd ~
adam@laptop:~$pwd
/home/adam
When we enter cd ~user’s_name the system will move us to that user’s home directory.
adam@laptop:~$cd ~zoidberg
adam@laptop:/home/zoidberg$pwd
/home/zoidberg
Another special character, namely -, denotes former directory. To jump to parent directory one needs to issue cd .. at the prompt. Every directory in Unix systems, even empty one, comprises of at least two parts . (dot) .. (double dot). One dot . means the same directory, double dots .. is a link to the parent directory in the directory tree.
adam@laptop:~$cd ..
adam@laptop:/home$pwd
/home
Together with cd command it is worthwhile to introduce two new expressions - the relative path and the absolute path.
- Absolute path is a path started from root of a directory tree “/”, e.g. /home/adam.
- Relative path is a path whose name begin with current directory, e.g. adam/ from /home/.
3. mkdir, rmdir
Both command deal with directories - the first one creates them, the second one deletes them. Both make use of one parameter - a directory path. rmdir can only be performed on empty directories.
adam@laptop:~/Documents/polishlinux.org/examples$ mkdir directory
adam@laptop:~/Documents/polishlinux.org/examples$ ls
directory example.txt all_about_console.txt
adam@laptop:~/Documents/polishlinux.org/examples$ rmdir directory/
adam@laptop:~/Documents/polishlinux.org/examples$ ls
example.txt all_about_console.txt
4. cp, mv, rm
- cp - (copy) is self explanatory, it uses at least two parameters: a source file and a target location to which the file will be copied,
- mv - (move) is used to change the location of directories and files or renaming them. It works in similar way as the cp command,
- rm - (remove) removes files/directories.
The commands: cp, mv, and rm have the following common parameters:
- -f force - forces removing a file, even if user has no rights to write the file,
- -i interactive - user will be asked to conform the operation,
- -b -backup - creates backup copy of a file to be overwritten (for cp and mv),
cp and rm commands can work in recursive mode - thanks to -r (-R, –recursive parameters.
adam@laptop:~/Documents/polishlinux.org/examples$ ls
directory1 directory2 directory3 example.txt all_about_console.txt
adam@laptop:~/Documents/polishlinux.org/examples$ cp -R directory2/ directory3/
adam@laptop:~/Documents/polishlinux.org/examples$ ls directory3/
directory2
adam@laptop:~/Documents/polishlinux.org/examples$ rm -r directory3/
adam@laptop:~/Documents/polishlinux.org/examples$ ls
directory1 directory2 example.txt all_about_console.txt
Beware! Command rm -r will remove ALL nested directories (not only empty ones), as in the above example.
5. ln
The “ln” utility creates a new directory entry (linked file) which has the same properties as the original file. Symlink (for short) can be thought as a sort of a short-cut known from Windows systems.
There are two kind of such links in Unix/Linux systems:
- symbolic links - referring to a file - they can be considered as Windows short-cuts,
- hard links - referring to a disk area, in other words to a physical disk area where a file is located.
If a file to which a symbolic link was created is removed (deleted), the symlink will be listed in red (provided the shell supports colors). If we delete a file with a hard link nothing will happen. The file won’t be erased until the number of hard links equals zero.
All symlinks are created by ln command issued with -s parameter.
adam@laptop:~/Documents/polishlinux.org/examples$ cat file
Symlink example.
adam@laptop:~/Documents/polishlinux.org/examples$ ln -s file
symlink_file
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
lrwxrwxrwx 1 adam adam 4 2007-06-01 19:11 symlink_file -> file
-rw-r--r-- 1 adam adam 22 2007-06-01 19:10 file
adam@laptop:~/Documents/polishlinux.org/examples$ rm file
adam@laptop:~/Documents/polishlinux.org/examples$ ls
symlink_file
The above listing shows symbolic link in action. ls (with -l) tells us the number of hard links for a “file” file remained the same. Contrary to the below example where number of two hard links had diminished to one after a file deletion and the “file” file was still present.
adam@laptop:~/Documents/polishlinux.org/examples$ ln file hardlink_file
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
-rw-r--r-- 2 adam adam 22 2007-06-01 19:11 hardlink_file
-rw-r--r-- 2 adam adam 22 2007-06-01 19:11 file
adam@laptop:~/Documents/polishlinux.org/examples$ rm file
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
-rw-r--r-- 1 adam adam 22 2007-06-01 19:11 hardlink_file
6. touch
This simple command has two applications. If we add as a parameter an existing file, the command will change the file’s modification time. If the file does not exist it will be created.
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l example.txt
-rw-r--r-- 1 adam adam 0 2007-05-30 11:31 example.txt
adam@laptop:~/Documents/polishlinux.org/examples$ touch example.txt
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
total 0
-rw-r--r-- 1 adam adam 0 2007-06-07 13:27 example.txt
-rw-r--r-- 1 adam adam 0 2007-05-30 11:31 all_about_console.txt
adam@laptop:~/Documents/polishlinux.org/examples$ touch new.txt
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
total 0
-rw-r--r-- 1 adam adam 0 2007-06-07 13:28 new.txt
-rw-r--r-- 1 adam adam 0 2007-06-07 13:27 example.txt
-rw-r--r-- 1 adam adam 0 2007-05-30 11:31 all_about_console.txt
When touch is invoked with -c or –no-create parameters we will prevent the file creation. Other parameters -d and -t change the access and modification times to the specified time. The “-t” needs to have a date specified in the form of [[CC]YY]MMDDhhmm[.ss].
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
total 0
-rw-r--r-- 1 adam adam 0 2007-06-07 13:28 new.txt
-rw-r--r-- 1 adam adam 0 2007-06-07 13:27 example.txt
-rw-r--r-- 1 adam adam 0 2007-05-30 11:31 all_about_console.txt
adam@laptop:~/Documents/polishlinux.org/examples$ touch -t
200706101200 new.txt
adam@laptop:~/Documents/polishlinux.org/examples$ touch -d
"last monday" example.txt
adam@laptop:~/Documents/polishlinux.org/examples$ touch -d
"2 days ago 12:00" all_about_console.txt
adam@laptop:~/Documents/polishlinux.org/examples$ ls -l
total 0
-rw-r--r-- 1 adam adam 0 2007-06-10 12:00 new.txt
-rw-r--r-- 1 adam adam 0 2007-06-04 00:00 example.txt
-rw-r--r-- 1 adam adam 0 2007-06-05 12:00 all_about_console.txt
7. df, du
Next shell commands - df and du - display free disk space - df for the whole filesystem and du for a given file. If run with -h attribute they will show the size in human readable format rather than blocks. For example:
adam@laptop:~/Documents/polishlinux.org/examples$ df -h
Filesystem Size Used Avail Capacity Mounted on
/dev/sda5 40G 34G 4,0G 90% /
varrun 502M 136K 502M 1% /var/run
varlock 502M 0 502M 0% /var/lock
procbususb 502M 148K 502M 1% /proc/bus/usb
udev 502M 148K 502M 1% /dev
devshm 502M 0 502M 0% /dev/shm
And example for the du command:
adam@laptop:~/Documents/polishlinux.org/examples$ du -h error.txt
4,0K error.txt
8. echo
This simple command returns a text or a message attached to it as a parameter.
adam@laptop:~$echo Hello World!
Hello World!
adam@laptop:~$
“Echo” adds at the end of a line a newline character. To get rid of it one should run the command with -n. For example:
adam@laptop:~$echo -n Hello World!
Hello World!adam@laptop:~$
When the text is enclosed in quotation marks it will be interpreted directly (but we will tell about it later).
9. pwd
pwd (print working directory) shows full path of the current directory.
adam@laptop:~$pwd
/home/adam
adam@laptop:~$
10. cat
cat command can be used for a file creation. To put it more correctly it can be used to redirect standard input to a file and to display it on standard output. Files are created in the following way:
adam@laptop:~$cat > file.txt
Very interesting text.
<Ctrl+D>
We will expand on the redirection operator > later in this guide. To display file contents it suffices to type in cat file_name.
adam@laptop:~$cat file.txt
Very interesting text.
One of its parameters prints line numbers when “cat” is used to shows a file contents.
adam@laptop:~$cat -n file.txt
1 Very interesting text.
cat enables us to concatenate several files together, indeed cat is shorthand for concatenate. In the following example five files are to be merged in one “file.iso” file.
cat file1 file2 file3 file4 file5 > file.iso
11. wc, head, tail
These three commands are used to process text strings. wc command displays a number of lines, a number of words, and a number of bytes for a given file.
adam@laptop:~/Documents/polishlinux.org$ wc all_about_console.txt
94 908 6828 all_about_console.txt
The numbers in the above example refer to (from left to right) - lines, words, and bits - found in all_about_console.txt file.
Two successive commands display accordingly: the head - a file beginning, and the tail - a file tail (ending). Both commands show 10 rows of a file by default. The number can be changed with the -n number parameter.
adam@laptop:~/Documents/polishlinux.org$ head -n 1 example.txt
The first row of the text.
adam@laptop:~/Documents/polishlinux.org$ tail -n 2 example.txt
The last but one row of the text.
The last row of the text.
12. less
less enables us to scroll a text up and down on the screen.
adam@laptop:~$less file.txt
Running the command as shown above will display file.txt contents on a screen. Entering :f during browsing the file will show interesting details, for example the line number of the line displayed at the upper edge of monitor’s screen or the file size. Joining the command with the “cat -n” command will display the contents supplemented by line numbers placed at the left side of a screen.
adam@laptop:~$cat -n file.txt | less
Entering :q quits the program (command).
This article is part of the Command line tricks series.
Go back to: Shell, terminal, console — the basics »
Go further to: UNIX Pipes, Streams and Redirections Explained »
Subscribe to RSS feed for this article!
9 Comments
- A hyperlink: <a href="polishlinux.org">GNU/Linux for everyone!</a>,
- Strong text: <strong>Strong text</strong>,
- Italic text: <em>italic text</em>,
- Strike: <strike>
strike</strike>, - Code: <code>
printf("hello world");</code>, - Block quote: <blockquote>Block quote</blockquote>










Maybe MMDDhhmm should be changed to [[CC]YY]MMDDhhmm[.ss] or the example should be change to 06101200.
… rather than blocks
… and a number of bytes for a given file.
what is command for listing out particular kind of files(for eg .cpp and .hpp) recursively according to the date of modification (earliest first and latest last) ????
Try: ls -Rlt | grep .*.cpp, if want to reverse output(latest first) use -r parameter.
This should do it:
find . -name ‘*.c’ -printf ‘%T@ %t %h%p\n’ | sort -n
Use sort -rn to reverse the order.
(The first number on every line is the modification time in a format sort will understand)
oops. that should of course be
-name ‘*.cpp’
Here’s one of my favourite commands:
du -a –max-depth=1 | sort -rn | head -11
It displays the size of the current directory followed by the top ten biggest things (which may be subdirectories) inside it.
Thanks for the explanation of the “ln” utility! Simple and to the point –> For the first time even I understand its use and the difference between “hard links” and “symbolic links”.
It might help to show the pwd command before the cd command, since the former is used in an example of the latter.
Is there a command for checking las modification date ?
or any simple way to check if a file has changed