Monday, June 3, 2013

This article is part of a unix shell scripting tutorial. We must know liitle things like below while writing shell scripts in linux.

Sometimes, we are not able to delete files from directory in Linux.
There are many reasons due to which we can’t delete that files as I have given
example. Below filename which I have mentioned, its name starts with – that’s
why we can’t delete it directly.

You can see just list of files before issue.

root@hello:~/test/hello#
ls

a.sh a.txt
file2 module test.html
-unable.txt

root@hello:~/test/hello#

I am creating file which will not delete by normal rm
command.

root@hello:~/test/hello#
cat > -unable.txt

Hello

How are you

root@hello:~/test/hello#

root@hello:~/test/hello#
ls

a.sh a.txt
file2 module test.html
-unable.txt

rm command facing the issue while deleting that file. We are
getting errors.

root@hello:~/test/hello#
rm "-unable.txt"

rm: invalid
option -- 'u'

Try `rm
./-unable.txt' to remove the file `-unable.txt'.

Try `rm --help'
for more information.

root@hello:~/test/hello#
rm -rf "-unable.txt"

rm: invalid
option -- 'u'

Try `rm
./-unable.txt' to remove the file `-unable.txt'.

Try `rm --help'
for more information.

root@hello:~/test/hello#

There is only one way to resolve this issue is that check
inode for that file by ls command with –i option. It will give us inode number
in first column for that file.

root@hello:~/test/hello#
ls -lrti

total 100

5770436
-rw-r--r-- 1 root root 80878 2012-11-03 18:00 test.html

5770345
-rw-r--r-- 1 root root 45 2012-11-05
21:59 file2

5770346
drwxr-xr-x 3 root root 4096 2012-11-05
22:35 module

5770437
-rw-r--r-- 1 root root 11 2012-11-09
16:50 a.txt

5770440
-rw-r--r-- 1 root root 128 2012-11-09
16:50 a.sh

5770079
-rw-r--r-- 1 root root 19 2013-06-03
22:04 -unable.txt

root@hello:~/test/hello#

Use above inode number in find command to search file and
delete it by using rm command inside the –exec option. Finally, we have
successfully deleted that file.

root@hello:~/test/hello#
find . -inum 5770079 -exec rm -rf {} \;

root@hello:~/test/hello#
ls

a.sh a.txt
file2 module test.html

root@hello:~/test/hello#

This case not belong to only the file
names whose name starts with – character but also in some other cases like- if
file not created properly then we can’t delete it etc. If you are not able to
delete files then just use this trick, it will be helpful for all cases.

It is very easy to check local network speed
in Windows but if you have putty access to Linux machine then we are facing
issues to check the network speed. There are some tools are available to check
network speed iin Linux but it is not possible to install this tools on client machine or
our secured machine.

Network speed in linux is depend on the Ethernet
card or network interface card which you are using for this machine. Might be
there are multiple Ethernet cards already configured, we can check network
speed for each separately.

Generally, Ethernet cards are configured.
If we see the the output of below command then we will get all NICs.

root@hello:~# ifconfig -a

eth0 Link encap:EthernetHWaddr 1c:6f:65:0b:00:eb

inet
addr:10.136.25.42
Bcast:10.136.25.255
Mask:255.255.255.0

inet6 addr:
fe80::1e6f:65ff:fe0b:eb/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500
Metric:1

RX packets:1133376 errors:0
dropped:0 overruns:0 frame:0

TX packets:34516 errors:0 dropped:0
overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:11234117 (113.8 MB) TX bytes:9056744 (9.0 MB)

Interrupt:16
Memory:fc500000-fc520000

lo Link
encap:Local Loopback

inet addr:127.0.0.1 Mask:255.255.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:16436
Metric:1

RX packets:20442 errors:0 dropped:0
overruns:0 frame:0

TX packets:20442 errors:0 dropped:0
overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:2356951 (2.3 MB) TX bytes:2356951 (2.3 MB)

root@hello:~#

If we see in above output, there are two
cards configured. But eth0 is the main. So we need to check network speed for
it only.

Above command will be helpful to check system
is half duplex or full duplex. Dmesg command gives lots of information but it
is not that much of useful for us just grep needed information.

We can
check network speed of wireless card and chipset information.

root@hello:~#lspci | grep -i wireless

To test
internet connection speed from the console over ssh command line, we need to
install lftp and iperf packages. They are easily available. We can download rpm
of them or install it from yum repository.

After installation, download or upload data by lftp, pget
command or check network speed between two IPs by iperf command.

Find has many options available
like ctime i.e. creation time, type – type of file, newer – compare files and
decide, it is newer or older as compared to one file, inum- inode number.

Examples:

Search files by inode numbers with find command:
find inum

root@hello:~/ctier/examples#
find . -inum 5770071

./README.txt

root@hello:~/ctier/examples#

Search files newer as compared to given file:
find newer

root@hello:~/ctier/examples# find
. -type f -newer filename

root@hello:~/ctier/examples# find
. -type f -newer README.txt

Newer option helpful to get new files as
compared to given file timestamp.

Search files older as compared to given file:
find newer

root@hello:~/ctier/examples# find
. -type f !-newer README.txt

To reverse the output of 2nd
example, we need to put !mark before the –newer option. It will negate the
meaning of command after the mark.

Search files by uid: find uid

root@hello:~#find . -type f
-uidNumeric USERID

root@hello:~#find . -type f
-uid 1002

Search files whose creation time is older than
30 days

root@hello:~#find . -type f
-ctime +30

Search files whose creation time is within last
30 days

root@hello:~#find . -type f
-ctime -30

Search log files whose creation time is older
than 30 days and delete it

In this case, we need to search files whose
creation time is older than 30 days and need to provide this list of files to
the rm command with the help of –exec option. We can run any file manipulation
command after exec option e.g. ls –lrt, cat, wc etc.

Find command is most powerful command in linux to find files
as per our criteria. It is used not only
to search files but also to run different commands on those files. Mostly we
are searching files by modification time or access time of that file. So, in
this article, we will search files by this two parameters only. We will see
mtime and atime in detail with examples.

To find files who accessed in last 7 days

To find files whose access time is newer i.e. in last 7 days
only. Then we can search those files by following command.

root@hello:~/ctier/examples#
find . -type f -mtime -7

This command will be helpful to get only files i.e. type
option will help to get files or directories if you put d instead of f then it
will search for directories. And mtime i.e. modification time should be last 7
days only then it should be -7 and if you want to do reverse i.e. you need all
files whose modification time is 7 days before i.e. do not display the files
whose modification time is last 7 days then use +7.

mtime option we can use with
multiple find commands option like –newer, -type, -name, -depth etc.

Examples:

To find files whose access time is within last 7
days only

root@hello:~/ctier/examples# find
. -type f -atime -7

To search files whose access time is more than
15 days older

root@hello:~/ctier/examples# find
. -type f -atime +15

To search files whose modification time is
within last 7 days and till the 3 depth of directory

root@hello:~/ctier/examples# find
. –maxdepth 3 -type f –mtime -7

To search files whose modification time is last
30 days ok but access time should be within last 2 days only