Linux : Working with Files - Module1

Linux: Working with Files - Module1
Linux: Working with Files - Module1

Linux: Working with Files




Module1        Module2        Practice

This module will learn you how to recognize, create, remove, files using commands like file, touch, rm,

1) all files are case sensitive
Files on Linux/Unix are case sensitive. This means that FILE1 is different from
file1, and /lib/bin is different from /lib/Bin.
This example shows the difference between two files, one with upper case H, the other
with lower case h.

dhiru@dhiru:~/Linux$ ls
hello.txt Hello.txt
dhiru@dhiru:~/Linux$ cat hello.txt
This is the hello text file.
dhiru@dhiru:~/Linux$ cat Hello.txt
Welcome to the Hello text file.


2) everything is a file
A directory is a special kind of file, but it is still a (case sensitive!) file. Each terminal window (for example /dev/pri/dhir), any hard disk or partition (for example /dev/sda1) and any process are all represented somewhere in the file system as a file. It will become clear throughout this module that everything on Linux/Unix is a file.

3) file
The file utility determines the file type. Linux does not use extensions to determine the file type.
The command line does not care whether a file ends in .txt or .pdf.
As a system administrator, you should use the file command to determine the file type. Here are some examples on a typical Linux system.

dhiru@dhiru:~$ file img12.jpg
img12.jpg: JPG image data, 3840 x 1200, 8-bit/color RGBA, non-interlaced
dhiru@dhiru:~$ file /etc/passwd
/etc/passwd: ASCII text
dhiru@dhiru:~$ file HelloWorld.c
HelloWorld.c: ASCII C program tex
t

The file command uses a magic file that contains patterns to recognise file types.
The magic file is located in /usr/share/file/magic. Type man 5 magic for more information.
It is interesting to point out file -s for special files like those in /dev and /proc.

dhiru@dhiru~# file /dev/sda
/dev/sda: block special (8/0)
dhiru@dhiru~# file -s /dev/sda
/dev/sda: x86 boot sector; partition 1: ID=0x83, active, starthead...
dhiru@dhiru~# file /proc/cpuinfo
/proc/cpuinfo: empty
dhiru@dhiru~# file -s /proc/cpuinfo
/proc/cpuinfo: ASCII C++ program text


4) touch
4.1) create an empty file
One easy way to create an empty file is with touch.
This example starts with an empty directory, creates two files with touch and the lists those files.

dhiru@dhiru:~$ ls -l
total 0
dhiru@dhiru:~$ touch file1
dhiru@dhiru:~$ touch file2
dhiru@dhiru:~$ touch file3
dhiru@dhiru:~$ ls -l
total 0
-rw-r--r-- 1 dhiru dhiru 0 Jun 3 08:57 file1
-rw-r--r-- 1 dhiru dhiru 0 Jun 3 08:56 file2
-rw-r--r-- 1 dhiru dhiru 0 Jun 3 08:56 file3
dhiru@dhiru:~$


4.2) touch -t
The touch command can set some properties while creating empty files.Looking at the next example.

dhiru@dhiru:~$ touch -t 201707030000 dhir
dhiru@dhiru:~$ touch -t 200906291630 Hello.txt
dhiru@dhiru:~$ ls -l
total 0
-rw-r--r-- 1 dhiru dhiru 0 Jun 29 2009 Hello.txt
-rw-r--r-- 1 dhiru dhiru 0 Jun 3 08:57 file1
-rw-r--r-- 1 dhiru dhiru 0 Jun 3 08:56 file2
-rw-r--r-- 1 dhiru dhiru 0 Jun 3 08:56 file3
-rw-r--r-- 1 dhiru dhiru 0 Jul 3 2017 dhir
dhiru@dhiru:~$


5) rm
rm is used to remove a file. It is used with many such as -i, -rf.

5.1) remove forever
When you want to remove a file forever and you haven't need this file in future, use rm to remove it.
When you use rm to remove a file, the file is gone. Therefore, be careful when removing files!

dhiru@dhiru:~$ ls
Hello.txt file1 file2 file3 dhir
dhiru@dhiru:~$ rm Hello.txt
dhiru@dhiru:~$ ls
file1 file2 file3 dhir
dhiru@dhiru:~$


5.2) rm -i
To prevent yourself from accidentally removing a file, you can type rm -i.

dhiru@dhiru:~$ ls
file1 file2 file3 dhir
dhiru@dhiru:~$ rm -i file3
rm: remove regular empty file `file3'? yes
dhiru@dhiru:~$ rm -i dhir
rm: remove regular empty file `dhir'? n
dhiru@dhiru:~$ ls
file1 file2 dhir
dhiru@dhiru:~$


5.3) rm -rf
By default, rm -r will not remove non-empty directories.
However rm accepts several options that will allow you to remove any directory.
The rm -rf statement is famous because it will erase anything (providing that you have the permissions to do so).
When you are logged on as root, be very careful with rm -rf (the f means force and the r means recursive)
since being root implies that permissions don't apply to you. You can literally erase your entire file system by accident.

dhiru@dhiru:~$ mkdir test
dhiru@dhiru:~$ rm test
rm: cannot remove `test': Is a directory
dhiru@dhiru:~$ rm -rf test
dhiru@dhiru:~$ ls test
ls: cannot access test: No such file or directory
dhiru@dhiru:~$


Module1        Module2        Practice

Comments