Technology blog::Life hacks::Linux::Hardware::Gaming

Essential Guide to Working with Files in Linux

Basic file operations in Linux

By , 24th July 2011 in Linux

In this tutorial I am going to go through some of the command line tools used to perform basic operations on files, as well as reading and writing on the command line.
 

Before starting this tutorial, you should be comfortable opening up a command shell or switch to a console screen. My terminal emulator of choice is Terminator due to its good highlighting and ability to create split windows for multiple tasks.

The first command we are going to look at is the listing command LS. This command allows us to see the directory structure and the files, allowing us to traverse the file system.

Basic usage is to just type LS at the prompt. This will simply show all the files and directories in the current directory, as long as they are not hidden.

tim@ubuntu:~$ ls
Desktop  Documents  Picures  Videos  Downloads  Music  Public  Templates

The default listing is to show just file and directory names, which may not be ideal but it does save space on the screen.

There are a number of switches, or flags, which you can set to change the layout and formatting of the output.

ls -l - Shows a long listing with meta data
ls -li - Show a long listing with the inode number
ls -lh - Shows a long listing in human readable format
ls -a - Shows everything including hidden files beginning with a .
ls -ltr - Shows a long listing with files sorted by timestamp reverse order

These are examples of the most useful flags, a full list of flags can be seen using the command:

ls --help | less

If you find yourself often typing in the same flags over and over again (I prefer to use ls -lh instead of just ls) you can create an alias in your bashrc script. We'll cover this in more detail later on, but essentially edit the ~/.bashrc file, locate the alias section and add one for alias ls="ls -lh". Save and relog.

When using the ls -l format, you may notice a horrid block of seemingly random nonsense at the start of each line. This is called the file metadata and it has a specific format.

<strong>drwxr-xr-x.</strong> 110 root root 8192 May 14 12:11 /etc

The first character represents the type of file. This isn't anything to do with the file format (.jpg, .txt etc) it is the system file type. These are described in the table below.

Letter CodeDescription
-Normal files (mydocument.txt)
dDirectory (Downloads/)
lSymbolic links (DL -> Downloads)
bBlock devices (/dev/sda)
cCharacter devices (/dev/tty0)
pNamed pipe
sSocket

The next 9 set of seemingly random characters are the file permissions. We'll look at Linux permissions in the next tutorial.

Next is the number of hard links to this entry in the metadata. This is equivalent to the number of subdirectories minus two. Minus two because there are two default "files" created, '.' which represents the current directory and '..' which represents the parent directory. These are system generated and cannot be deleted.

The next two fields are the owner username and the owning group. Again, we'll cover these in greater detail when we look at permissions in Linux.

Next is the file size, by default in bytes but by specifying -h in the command this is converted to a human readable format with units, so 4096 becomes 4.0k.

Next, we have the last modified timestamp, then the actual name of the file.

Finally, we can provide another parameter to the ls command, to view the contents of other folders.

ls -h /dev

Now that we can see what files and directories we can have a look at working with the files. There are simple commands for copying, moving, renaming and deleting files.

CommandDescription
cpFile Copy
mvMove or rename files
rmDelete files

Each of these commands has a few common flags which are really useful to know about.

FlagAction
-iInteractive mode (prompts for confirmations)
-rRecursive (processes all files and directories below the one specified)
-vVerbose (more visual indication as to what's going on)
*The Wildcard (matches anything)

Copy Files

There are several combinations of file copy operations. The basic syntax is

cp source destination

To make things easier you can use the asterisk character as a wild card. These allow multiple files to be copied. Here are some examples.

cp * /tmp/ - copy everything
cp *temp /tmp/ - copy everything ending with temp
cp temp* /tmp/ - copy everything starting with temp
cp *.php /usr/web/public_html - copy all php files to the public_html folder

Recursive File Copy

By default, the cp command does not recurse into folders. For that, you need to add the -r flag. This will copy matching files in the source directory and subdirectories, creating a new structure on the destination.

(add examples of recursive directory copy)

Change directory

If you stayed in the home all the time, life would be pretty boring. You can use the CD command to change the current working directory to another on the file system. You can specify a directory withing the current directory, an absolute path or a relative path.

cd Documents - change to the Documents directory of the current folder
cd /usr/bin - change to an absolute directory
cd .. - change to the parent direectory
cd ../Documents - change to the documents directory of the parent directory
cd ../../../Documents - change to the documents directory of the great grandparent directory

If all this gets too much for you, you can use the ~ (tilde) to return home.

cd ~

Create Directory

You may be wondering where all these directories come from and if you can create your own. Well, you can create your own using the mkdir command.

mkdir myDirectory

This will create a new directory in the current directory called myDirectory.

You can also create multiple directories or directories in other paths by specifying an absolute or relative path. If there are multiple directories which do not exist, you need to specify the -p flag to create parent paths as well.

mkdir some/test/path
mkdir: cannot create directory 'some/test/path': no such file or directory
mkdir -p some/test/path

We'll look at permissions in depth in a later tutorial, but you can specify the permissions when creating a directory.

mkdir -m 700 testfolder

Will create the testfolder with the permissions set to 700.

Remove or Delete a File

It's always good to remove unwanted files, and this is easy with the remove (rm) command.

rm file

Wildcards can also be used as per the copy command.

Remove Directory

Folders can be removed as long as they are empty, but it isn't really very nice to have to go into a directory and manually delete all the files first, especially if the directory contains subdirectories which also must be cleansed of files.

rm: cannot remove 'someDirectory': Is a directory

You can use the flags for recursive deletion, with a forced removal. This will recursively delete all files and directories, and force the removal.

rm -rf someDirectory

Be careful using this command, especially if you have root privileges, as you can delete the entire system of directories which may result in an unstable or broken environment

Update Last Modified Timestamp

Linux maintains a timestamp on a file which is the last time it was modified. This is useful for archiving, where you can specify to archive all files modified since a specified date. Should you wish to backup a file whose date is previous to this you can either open the file and save it or use the touch command.

touch myFile

This will update the timestamp to the time now. If the file does not exist it will be created as a zero length file.

Further Reading
Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.