Vous êtes sur la page 1sur 15

98Find files on a specific file system

If you know the file name and file system but not sure the exact
folder path then you can use this syntax. In below example, I am
searching for messages file in /var file system.

[root@Chandan ~]# find /var -name messages

/var/log/messages

[root@Chandan ~]#

Tips: if you dont know the file system name, you can search on /
level but keep in mind it may take time if you have a large number
of file systems.

[root@Chandan ~]# find / -name messages

/var/log/messages

[root@Chandan ~]#

If you dont know the exact file name, you can also use wildcard
pattern to search. For ex to search error_log you may try

[root@Chandan ~]# find / -name error_*

/var/log/httpd/error_log

[root@Chandan ~]#

How about searching file name with lower or upper case in other
word ignoring case sensitive? Well, you can use iname instead
of name. For ex:-
[root@Chandan var]# find / -iname MESSAGES

/var/log/messages

[root@Chandan var]#

Lets take a look at one more real-time scenario. If you know the
file type and want to search all of them. For ex if you are working
on WebSphere, you may want to search all files ending with .out
then you can try

# find / -name *.out

Find files based on ownership and permissions


Having files with 777 permission is dangerous as anyone can edit
or delete so as a System Administrator you may want to put a scan
in place to find any files with 777 permissions. For ex to show any
files having 777 permission under /opt file system.

[root@Chandan ~]# find /opt/ -type f -perm 777

/opt/testing

/opt/SystemOut.log

[root@Chandan ~]#

Tips: how about printing file ownership, the time stamp in same
line command?

[root@Chandan ~]# find /opt/ -type f -perm 777 -exec ls -ltr {}


;

-rwxrwxrwx 1 root root 0 Jul 19 03:35 /opt/testing


-rwxrwxrwx 1 root root 0 Jul 19 03:36 /opt/SystemOut.log

[root@Chandan ~]#

You may also change permission from 777 to 755 in single find
command syntax.

# find /opt/ -type f -perm 777 -exec chmod 755 {} ;

Obviously, you can adjust permission from 755 to any other you
may like.

How about finding files, which is owned by root or different user?


This is very helpful if you are having issues while starting the
services due to the previous start was done by root. For ex
if tomcat is owned by a user called tomcatapp and for some
reason, you have started with root.

Guess what will happen when you restart next time with
tomcatapp? It wont because some of the files ownership is
changed to root and now tomcatapp cant modify/delete those
files. So this becomes very handy in that situation. Here is how you
can search any file owned by root in specific file system.

# find /opt/ -user root

Note: performing this find syntax on / level will results so many


files/folders so you may want to control by doing this in specific
file system.

Find files older than particular days


File System housekeeping is essential for production support and
often you have to deal with this syntax to find logs which are older
than (lets say) 60 days. Below example is to find access.log file
older than 60 days in /opt file system.

# find /opt/ -name access.log -mtime +60

Tips: if you decide to find and delete in same command line you
can do like below. This will find access.log older than 60 days in
/opt file system and delete it.

# find /opt/ -name access.log -mtime +60 -exec rm {} ;

While this is very handy, you may want to list the files before you
delete them. To do so

# find /opt/ -name access.log -mtime +60 -exec ls -ltr {} ;

Find large file size


Sometime you may have to deal with frequent file system cleanup
due to large number of logs are being written by application due to
a code issue, etc. Lets take an example of searching file greater
than 1 GB in /opt file system.

# find /opt/ -size +1G

Tips: If you know all files in /opt/ with more than 1 GB can be
deleted then you can just have find and delete in same line.

# find /opt/ -size +1G -exec rm {} ;

I hope above find commands are handy to you and help you in real-
time.
Middleware was second hottest skills of 2014 and there is no
reason to go down in coming years. If you recently started working
on WebSphere or any other product suite of Middleware then one
of the very first things to get familiar is widely used Linux
commands.

In this article, I will talk about some of the often-used Linux


commands by WebSphere or Middleware administrator on a daily
basis.

Finding SystemOut.log

If you are working on existing environment and if an administrator


has redirected SystemOut.log to some other location then it would
be challenging to find it.

#find / -name SystemOut.log

Tips: if you are having multiple file system then it may take a time
to search. So best would be to replace / with actual file system
where you think the log would be. Lets say you believe log is in
/opt file system so you can do like this.

#find /opt name SystemOut.log

Know which process is holding specific port number

Quite often you will have to deal with port conflict issue, especially
on the shared environment. If there is a situation to find out which
process is holding port number then here is how you can know.

#netstat anlp | grep 443

[root@Chandan ~]# netstat -anlp | grep 443


tcp 0 0
0.0.0.0:443 0.0.0.0:* LISTEN
20924/nginx

[root@Chandan ~]#

Note: Above example shows nginx with PID 20924 is


holding 443 port.

Server boot time

If you are performing auto-startup troubleshooting and would like


to know when the server was rebooted you can use this command.

#who b

[root@Chandan ~]# who -b

system boot Jun 28 01:11

[root@Chandan ~]#

who -b command will give you exact date and time of server
reboot.

Alternatively, you may also use up time to check how long the
server is up.

[root@Chandan ~]# uptime

01:20:27 up 14 days, 9 min, 1 user, load average: 0.00, 0.00,


0.00

[root@Chandan ~]#
Check CPU/Memory utilization in runtime

If you are having performance issue then you might want to know
the current CPU/Memory utilization. This will help you to find out
which process is taking high CPU/Memory in real-time.

#top

top - 01:16:21 up 14 days, 5 min, 1 user, load average: 0.00,


0.00, 0.00

Tasks: 70 total, 1 running, 69 sleeping, 0 stopped, 0


zombie

Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.3%si,


0.0%st

Mem: 502220k total, 454920k used, 47300k free, 143476k


buffers

Swap: 0k total, 0k used, 0k free, 245412k


cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+


COMMAND

29121 root 20 0 15008 1260 992 R 0.3 0.3 0:00.02


top 1 root 20 0 19232 1136 860 S 0.0
0.2 0:25.18 init 2
root 20 0 0 0 0 S 0.0 0.0 0:00.00
kthreadd 3 root RT 0 0 0 0 S
0.0 0.0 0:00.00 migration/0 4
root 20 0 0 0 0 S 0.0 0.0 0:01.21 ksoftirqd/0

Tips: look for CPU/Memory section for first few PID to find out the
utilization.

Alternatively, you may also use free command to find out total
and free memory.
#free m

[root@Chandan ~]# free -m

total used free shared buffers


cached

Mem: 490 444 46 0 140


239

-/+ buffers/cache: 64 425

Swap: 0 0 0

[root@Chandan ~]#

As you can see above, there is 490 MB total memory and only 46
MB memory is available.

There is another command called sar (System Activity Report),


which is also very helpful to find the CPU and Memory stats.

To check the CPU utilization

#sar

To check the Memory utilization

sar -r

Kill the process

The sometime process doesnt stop gracefully if its hung or


defunct. In this scenario, you can kill the process manually.

#kill PID
If above doesnt help, you can use -9 to kill the process forcefully.

#kill -9 PID

Note: PID is your process ID

Compression & Extraction

Most often you will have to deal with compressing the files as
housekeeping activity for a file system.

Compression

gzip command can be used to compress the files.

#gzip filename

Tips: if you have multiple files to be compressed, you can use


gzip * which will compress all the files available in that working
directory.

Extraction

gunzip command will help you to extract the gz file.

#gunzip filename

Tips: you can use gunzip * which will help you to extract all gz file
in that working directory.
Total CPU, Memory, File System

Often asked to provide server information, if you are working on


migration or capacity planning. Here is the quick command to help
you to find out total CPU, Memory, and File system details.

CPU information

Find out CPU Cores, MHz, CPU manufacturer, Model Name and
much more by using

#cat /proc/cpuinfo

Tips: if you are having a high-end server then an output of above


command will be long. So you can use grep to filter out the
required information. For ex, just to find out cores you can use

#cat /proc/cpuinfo | grep cores

Memory information

To find out total available memory, you can use one of the
following commands.

First one.

#free G

This will show you total, free & cached memory

Second one

#cat /proc/meminfo
This will show you much more information along with total
memory.

File System information

To find out file system size and usage, you can use df commands.

#df h

[root@Chandan tmp]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/vda1 20G 1.6G 18G 9% /

tmpfs 246M 0 246M 0% /dev/shm

[root@Chandan tmp]#

Including h will give you output in GB which is easy to


understand.

Check out IP, Subnet Mask, MAC address and errors

Probably, one of the most widely used commands to find out IP


related information.

[root@Chandan tmp]# ifconfig

eth0 Link encap:Ethernet HWaddr 04:01:5A:25:57:01

inet addr:128.199.100.162 Bcast:128.199.127.255


Mask:255.255.192.0

inet6 addr: fe80::601:5aff:fe25:5701/64 Scope:Link


UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:1491330 errors:0 dropped:0 overruns:0


frame:0

TX packets:1636419 errors:0 dropped:0 overruns:0


carrier:0

collisions:0 txqueuelen:1000

RX bytes:306418374 (292.2 MiB) TX bytes:274350737


(261.6 MiB)

[root@Chandan tmp]#

As you can see above, ifconfig will show the IP addresses, Ethernet
details, a hardware address (MAC), subnet mask, errors and other
details. This is the extremely useful handy command if you are
looking for this information.

Tips: you can use a with ifconfig to show all available Ethernet
details. Ex:

#ifconfig a

Network commands like wget, telnet, traceroute

If you are doing application connectivity troubleshooting then most


likely you will need to use these network commands.

Check if you can access particular URL from server

You can quickly confirm if there is any connectivity issue on your


server by using wget command. Ex: if you need to check if the
server can access external website like https://geekflare.com or
not, you can use below.
[root@Chandan tmp]# wget geekflare.com

--2015-07-12 02:52:56-- https://geekflare.com/

Resolving geekflare.com... 104.28.23.60, 104.28.22.60

Connecting to geekflare.com|104.28.23.60|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: unspecified [text/html]

Saving to: `index.html'

[
<=>
] 65,435 --.-K/s in 0.005s

2015-07-12 02:52:56 (13.4 MB/s) - `index.html' saved [65435]

[root@Chandan tmp]#

Tips: above you can see HTTP response code is 200 means
there is no issue in connectivity.

Verify if server can connect to backend service with


particular port number

If you are connecting to some other application like web service


and need to verify if you can reach them then you can use telnet
command. This is very useful for connectivity test. In below
example, I have checked if geekflare.com on 443 port can be
reached or not.

[root@Chandan tmp]# telnet geekflare.com 443


Trying 104.28.23.60...

Connected to geekflare.com.

Escape character is '^]'.

If you see Connected then that confirms there is nothing wrong


with connectivity/firewall. However, if you see connection failed
then you know there is something like a firewall blocking your
connection.

Tracing connection details

This may not be used all the times but extremely helpful when you
have latency issue or just want to find out the connection path
between your server to the destination. Below example is to show
the network path from my server to geekflare.com

[root@Chandan tmp]# traceroute geekflare.com

traceroute to geekflare.com (104.28.23.60), 30 hops max, 60 byte


packets

1 128.199.127.253 (128.199.127.253) 5.110 ms 5.061 ms


128.199.127.254 (128.199.127.254) 0.418 ms

2 103.253.144.237 (103.253.144.237) 5.153 ms 0.463 ms


103.253.144.241 (103.253.144.241) 8.009 ms

3 as13335.singapore.megaport.com (103.41.12.6) 4.822 ms


13335.sgw.equinix.com (202.79.197.132) 4.819 ms
as13335.singapore.megaport.com (103.41.12.6) 4.490 ms

4 104.28.23.60 (104.28.23.60) 4.454 ms 4.427 ms 4.366 ms

[root@Chandan tmp]#
Ownership modification

If you are dealing with multiple users on a server and having root
permission too then most likely once in a while, you will screw with
permission/ownership. Well, not to worry its part of learning. Here
is the quick way to change the ownership or permission.

Change ownership

To change ownership of a particular file, you can use chown


command as below.

#chown user:group error.log

Tips: above will change ownership of error.log to mentioned user


and group. If you have a folder then you can use R, which will
change recursively to all the files within the folder. Ex:-

#chown R user:group somefolder

Vous aimerez peut-être aussi