How to find the directory a device is mounted to? - linux

In Linux, is it possible via a bash script to take '/dev/sr0' and determine what folder it's mounted to and perform extra actions on said folder?

You can output the directory name via this command:
grep /dev/sr0 /etc/mtab | cut "-d " -f2
or use it in other commands like this (e.g. list its contents):
ls $(grep /dev/sr0 /etc/mtab | cut "-d " -f2)

You can use the command mount(8) to find what device is mounted where. On Linux this information is found in the /proc/mounts file.

Related

Unsing integer Variable to process linux cut command fields

The following command below does not succeed.
for i in {1..5} ; do cat /etc/fstab | egrep "(ext3|ext4|xfs)" | awk '{print $2}' | cut -d"/" -f1-$i ; done
It seems that $i is ignored completely. It always returns instead result of
cut -d"/" -f1-
Any idea why it fails?
Thanks in advance!
The command itself is a part of a script that should help me to auto re-arrange fstab lines to match the right mount order (like /test/subfolder must come after /test was mounted and not before).
I tried and it didn't work for zsh shell. BUT I tried it in bash and it does work, so if you are using zsh just run the command with bash and it should work ;)

Grep in Ubuntu 14.04

I just recently updated my Ubuntu VM to 14.04 and before this I could run the grep command
grep -nr "piece of text"
or any other grep command for that matter and it would finish in 2-10 seconds in the directory I was working on. Now for some reason (no idea if this was caused by the update) running any grep command in the same dir I was working in just seems to hang there (idk if it does complete because I don't want to wait over a min or so for a search to happen) instead of showing my results. Any idea of what's happening or what I can do and try to fix it?
You aren't specifying the files to grep, so it's trying to read STDIN. I think you wanted
grep -nr "piece of text" *
Note the asterisk is globbed to all files in the current path.
which grep are you using ? -r option default to '.' directory in gnu case
which grep :
/bin/grep --version
/bin/grep (GNU grep) 2.18)
...
this is gnu grep :
echo "truc" >/tmp/truc
cd /tmp; grep -nr "truc";
using gnu grep -> it search recursively files in . of current directory ( here in /tmp ) and displays them containing "truc"
if /bin/grep --version is busybox :
BusyBox v1.22.1 (Debian 1:1.22.0-8) multi-call binary.
Usage:
grep [-HhnlLoqvsriwFEz] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]...
using busybox : cd /tmp; busybox grep -nr "truc";
wait indefinitely that you type content of file on STDIN ( stop with Ctrl+C ).
it reads STDIN as a file ( not even caring that -n indicates a directory recursion ).
As others answered, and as POSIX grep or GNU grep documentation tells, you need to specify some file to the standard grep command. (But GNU grep defaults to current directory . if -r is given without a directory name; however if -r is not given at all, grep defaults to read - i.e. the stdin).
BTW, I would also recommend the ack utility, packaged as the ack-grep package on Ubuntu or Debian. It does not need any files (default is the source code files under current directory) and it avoids useless non-source files (version control or object files...):
ack "piece of text"
Also, under emacs, you can use M-x grep

how to do this in bash script

in Linux, suppose mount command return this
/dev/sdc1 on /media/ELF (^-^)V type vfat
/dev/sdb1 on /media/PENDRIVE type vfat
all I want to do is get all mount point of my usb disk.
I did that already, using combination of grep and sed I can get these:
/media/ELF (^-^)V
/media/PENDRIVE
the problem is, when I do for loop in bash, those text will become 3 part instead of 2 parts , I mean :
suppose I put the result of those text in LIST
for list in $LIST; do
echo $list
done;
the result of that for loop becomes
/media/ELF
(^-^)V
/media/PENDRIVE
how to handle this issue? or are there any easier ways to get mount point of my usb disk?
Thanks
If you've already extracted the mountpoint paths and the only issue is to process them in a loop:
while read -r mountpoint; do
echo "[$mountpoint]"
done < <(mount | grep /media | grep uhelper=udisks | sed -e 's/\/dev\/.*on //g' -e 's/ type .*//g')

compare mount and /etc/filesystems in AIX

I want to compare the output put of mount command to the /etc/filesystems. Basically we want to validate everything is getting mounted properly as defined in /etc/filesystems after any system change(reboot etc.)
My basic script is:
#!/bin/bash
mountpoint="/vol/test/abc"
if grep -qs "$mountpoint" /etc/filesystems; then
echo "good"
else
echo "bad"
fi
Is this right approach? Please suggest. Also How can I get all the volumes that are being returned by executing mount command?
I think the general approach you want is to first generate a list of all the filesystems that you expect to be mounted, by looking at /etc/filesystems (use some combination of awk, grep, etc. to get just the names).
Then, get the list of filesystems that are actually mounted by running the mount command with no arguments.
Finally, compare the original list with the second list, and make sure nothing is missing.
As z242 suggested:
# Matching lines from /etc/filesystems
sed -n 's%^\(/.*\):%\1%p' /etc/filesystems | sort -o f1
# Matching lines from mount command
mount | tail +3 | awk '{print $2}' | sort -o f2
# Now compare the two
comm -3 f1 f2
Items listed with no indent are those in /etc/filesystems but not mounted. Items listed with an indent are those mounted but not in /etc/filesystems. If you don't care about the latter change comm -3 to comm -23

Identify the files opened a particular process on linux

I need a script to identify the files opened a particular process on linux
To identify fd :
>cd /proc/<PID>/fd; ls |wc –l
I expect to see a list of numbers which is the list of files descriptors' number using in the process. Please show me how to see all the files using in that process.
Thanks.
The command you probably want to use is lsof. This is a better idea than digging in /proc, since the command is a more clear and a more stable way to get system information.
lsof -p pid
However, if you're interested in /proc stuff, you may notice that files /proc/<pid>/fd/x is a symlink to the file it's associated with. You can read the symlink value with readlink command. For example, this shows the terminal stdin is bound to:
$ readlink /proc/self/fd/0
/dev/pts/43
or, to get all files for some process,
ls /proc/<pid>/fd/* | xargs -L 1 readlink
While lsof is nice you can just do:
ls -l /proc/pidoftheproces/fd
lsof -p <pid number here> | wc -l
if you don't have lsof, you can do roughly the same using just /proc
eg
$ pid=1825
$ ls -1 /proc/$pid/fd/*
$ awk '!/\[/&&$6{_[$6]++}END{for(i in _)print i}' /proc/$pid/maps
You need lsof. To get the PID of the application which opened foo.txt:
lsof | grep foo.txt | awk -F\ '{print $2}'
or what Macmede said to do the opposite (list files opened by a process).
lsof | grep processName

Resources