A Little About Linux: Basic File Management Tools

So, I'll start off this section with some information that you may not know.  When you use Linux, you are not using Linux... OK let me clarify that, according to the Free Software Foundation what you're actually using is GNU/Linux.  You're confused I can tell, so let me explain even further.  You see officially, Linux is only the kernel and if you want the distribution to do anything useful, you also require a selection of core applications to copy, move and list files etc.  It's these files which make up the 'GNU' bit of GNU/Linux and it's what the information on this page is all about.

List Files and Directories
One of the best places to start is learning how to list files and directories.  ls (list) is a command you will use every day for this purpose and it's handy to know some of the parameters you can use it with.

Running ls on it's own gives you a basic list of all files and directories;
# ls


bin  boot  dev  etc  home

Adding -l gives more information;
# ls -l

dr-xr-xr-x.   2 root root  4096 Nov 22 16:25 bin
dr-xr-xr-x.   5 root root  4096 Nov 22 16:26 boot
drwxr-xr-x.  19 root root  3860 Dec 26 17:11 dev
drwxrwxr-x.  82 1000 1000  4096 Dec 26 17:11 etc
drwxr-xr-x.   6 root root  4096 Nov  9 07:09 home

Adding a -h shows the size of the files and directories in human readable format rather than bytes;
# ls -lh

dr-xr-xr-x.   2 root root 4.0K Nov 22 16:25 bin
dr-xr-xr-x.   5 root root 4.0K Nov 22 16:26 boot
drwxr-xr-x.  19 root root 3.8K Dec 26 17:11 dev
drwxrwxr-x.  82 1000 1000 4.0K Dec 26 17:11 etc
drwxr-xr-x.   6 root root 4.0K Nov  9 07:09 home

Adding a -a shows all files including hidden ones;
# ls -lha

drwxr-xr-x.  24 1000 1000 4.0K Dec 26 17:11 .
drwxr-xr-x.  24 1000 1000 4.0K Dec 26 17:11 ..
-rw-r--r--.   1 root root    0 Dec 26 17:11 .autofsck
dr-xr-xr-x.   2 root root 4.0K Nov 22 16:25 bin
dr-xr-xr-x.   5 root root 4.0K Nov 22 16:26 boot
drwxr-xr-x.  19 root root 3.8K Dec 26 17:11 dev
drwxrwxr-x.  82 1000 1000 4.0K Dec 26 17:11 etc
drwxr-xr-x.   6 root root 4.0K Nov  9 07:09 home

Normally, the file listing is shown in alphabetical order, add -t to sort by last modified time, -r to reverse the order so that last modified is at the bottom of the list.  Also, you can add -R to specify a recursive file listing down a particular path i.e;

# ls -lhatrR home/

home/palindrome:
total 36K
-rw-r--r--. 1 palindrome palindrome  124 Sep 26 03:13 .bashrc
-rw-r--r--. 1 palindrome palindrome  176 Sep 26 03:13 .bash_profile
-rw-r--r--. 1 palindrome palindrome   18 Sep 26 03:13 .bash_logout
drwxr-xr-x. 6 root       root       4.0K Nov  9 07:09 ..
drwxr-xr-x. 2 palindrome palindrome 4.0K Nov  9 07:29 .vim
-rwxrw----. 1 johnga     palindrome   16 Nov  9 07:29 hello.txt
-rw-------. 1 palindrome palindrome 1.3K Nov  9 07:34 .viminfo
drwx------. 3 palindrome palindrome 4.0K Nov  9 07:34 .
-rw-------. 1 palindrome palindrome  441 Nov  9 08:04 .bash_history

home/palindrome/.vim:
total 12K
-rw-rw-r--. 1 palindrome palindrome   92 Nov  9 07:29 .netrwhist
drwxr-xr-x. 2 palindrome palindrome 4.0K Nov  9 07:29 .
drwx------. 3 palindrome palindrome 4.0K Nov  9 07:34 .. 

Copying Files
To copy files, use the cp command.  The format of this command is cp [options] source_location destination_location;

Maintain the file permissions and ownership of a file using -p.  You will probably want to use this parameter as default as without it, the permissions and ownership are set to the user account initiating the copy;

# cp -p /home/palindrome/hello.txt /tmp/

Force an overwrite of files that already exist using -f;

# cp -f /home/palindrome/hello.txt /tmp/

By specifying the directory as the source, you can perfom a complete recursive copy of all files and directories that lie beneath that point.  Use -R for this (Some implementations also use -r);

# cp -r /home/palindrome/ /tmp/

You can use the archive option -a which is similar to the -R option but maintains all permissions and copies any links as is.

# cp -a /home/palindrome/ /tmp/

Finally, only copy files when the source files are newer than the destination, use -u;

# cp -u /home/palindrome/ /tmp/

Moving and Renaming Files
So, the next command is mv (Move) and this handles the moving and renaming files and directories, because when you think about it, renaming a file is just like moving a file to the same place and giving it a different name.  The options you can use in conjunction with the mv command are very similar to the cp command.  

So if I want to move a file called hello.txt from the current folder to /home/plaindrome/ try;

# mv ./hello.txt /home/palindrome/

If you want to rename the file hello.txt to hello2.txt and keep in the same directory try;
# mv ./hello.txt hello2.txt

Deleting and Removing Files
The rm command (Remove) is the generic command for deleting files and all folders containing files'

Delete the test folder and all its file contents, all folders underneath (recursively) and don't prompt;
# rm -rf /home/palindrome/test/

Creating a Directory
Create any directory using the mkdir command (Make Directory).

# mkdir my_directory

Delete/Remove a Directory
Delete any directory using the rmdir command (Remove Directory).

# rmdir my_folder

Touch
The touch command is truly a unique command to Linux, there is no Windows alternative.  The purpose is to change a time stamp on a file.  It will also create a 0 byte blank file if just run with a non-existent file name.  Linux keeps track of three different time stamps;
  • Created
  • Last Modified
  • Last Accessed
Alter only the Last Modified date;
# touch -m test.txt

Alter only the Last Accessed date;
# touch -a test.txt

TAR Archiving
The TAR file (Tape Archive) is a way of collecting a group of files into a package.  Normally these packages are then further compressed using gzip or bzip2 and then becomes 'tarballs'.  The most common tar options are listed below;

  
Package files into a TAR files.  Put the contents of the 'directory' directory into the new archive.tar file.  -c Create new Archive, -v Be Verbose, -f Archive file name.

tar cvf archive.tar directory/

Do as above but also gzip the finished tar file. -z Gzip the tar file.

tar cvzf archive.tar.gz directory/

Extract a complete Gzipped tarball into 'directory'. -x Extract files from an archive.

tar xvzf archive.tar.qz  directory/

Copy Files To and From Archives
Similar to TAR but different in operation.  The way we locate the files to add to a CPIO archive is to use standard input, in other words, you can use 'ls' to list the files in a directory and then pipe ito into cpio or search for some files and pipe it into cpio.  Some exapmples are listed below;

Using 'ls' to populate a CPIO archive. -o Create archive in Copy-Out Mode;
ls
test1.txt test2.txt test3.txt

ls | cpio -o > /tmp/test.cpio

Using 'find' to populate a CPIO archive;
find ./directory | cpio -o > /tmp/test.cpio

Using 'find' to populate a CPIO archive, then Gzipping it;
find ./directory | cpio -o | gzip > /tmp/test.cpio.gz

Extracting a standard CPIO archive. -i Extract archive in Copy-In Mode;
cpio -i < /tmp/test.cpio

Extracting a Gzipped CPIO archive;
gunzip -c /tmp/test.cpio.gz | cpio -i

GZIP Archiving
We've touched on using GZIP earlier when created tarballs.  GZIP is a compression system that uses the Lempel-Ziv coding (LZ77) algorithm, which means it's compatible with zip programs on other platforms such as Windows.  There are essentially two commands that you will need to perform GZIP compression; gzip to compress and gunzip to uncompress.

In its basic format, you can compress files using;
gzip install.log

You can gzip multiple files at the same time;
gzip install.log install2.log install3.log

This will have created a file called install.log.gz and deleted the original install.log file.  Want to keep both?  Try;
gzip -c install.log > install.log.gz

You will now have both the original and compressed files. To compress all files in any directory structure, use the recursive -r option.  This will start at the source directory and any file located beneath that hierarchy with be compressed.
gzip -r top_directory

To uncompress simply use;
gunzip install.log.gz

Or to uncompress multiple files use;
gunzip install.log,gz install2.log.gz install3.log.gz

To uncompress a series of gzipped files located in a series of hierarchical directories use -r in the same way you did to gzip the files eralier;
gunzip -r top_directory

BZIP2 Archiving
Using the Burrow-Wheeler algorthim, BZIP2 is much more efficient in compressing files than GZIP, but is considerably slower.  As with GZIP, the commands you will need are bzip2 and bunzip2.

In its basic format, you can compress files using;
bzip2 install.log

You can bzip2 multiple files at the same time;
bzip2 install.log install2.log install3.log

This will have created a file called install.log.bz2 and deleted the original install.log file.  Want to keep both?  Try;
bzip2 -c install.log > install.log.bz2

To uncompress simply use;
bunzip2 install.log.bz2

Or to uncompress multiple files use;
bunzip2 install.log.bz2 install2.log.bz2 install3.log.bz2


Determining a File Type
If you are ever in a position when you find a file on a Linux file system and you don't know what it is, the file command is your friend.  Under standard day to day usage, it has a simple usage;
file install.log

Output;
install.log: ASCII text

Or for a bzip2 file;
file install.log.bz2

Output;

install.log.bz2: bzip2 compressed data, block size = 900k

No comments :