Using grep and * to get the exact file name - linux

I have following heapdump files
AppSrv01]# ls -ltr *heapdump*
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3436
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3435
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3434
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3433
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3432
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3431
-rw-r--r-- 1 root root 0 Sep 13 17:03 heapdump.dfsdf
now if I use
ls -ltr| grep heapdump*
I get following output
AppSrv01]# ls -ltr |grep heapdump*
-rw-r--r-- 1 root root 0 Sep 13 17:03 heapdump.dfsdf
But I use
ls -ltr |grep *heapdump*
I get no output. Could anybody help where is my mistake and how to search the heapdump files.
My expected output is
ls -ltr | grep *heapdump*
should give me
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3436
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3435
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3434
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3433
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3432
-rw-r--r-- 1 root root 0 Sep 13 16:44 hbsbdheapdump.3431
-rw-r--r-- 1 root root 0 Sep 13 17:03 heapdump.dfsdf
due to some reason I could not use
ls -ltr *heapdump*

It looks like you're confusing glob patterns with regular expressions, which is what grep uses.
The correct way to print a list of files containing the word heapdump is like this:
printf '%s\n' *heapdump*
Here, *heapdump* is glob-expanded by the shell to match anything followed by "heapdump" followed by anything else. All the files that match the pattern are passed to printf, which prints each one followed by a newline.
If you want to filter the output of ls -ltr, then you can use:
ls -ltr *heapdump*
The same list of files will be passed to ls, which will print information about them.
Bear in mind that it is not recommended to attempt to parse the output of ls.

Related

linux find files by created in last X minutes + sort does not return expected list

I have a folder with many jpg files, where each file name presents the creation time in the form of: "20220204T160311.746....jpg" ("..." represent additional data).
I'm trying to get all files created on the last X minutes. There are tons of results for such query if one asks Google, nevertheless, I'm getting weird results when trying the following:
find . -cmin -50 \( ! -iname "*.csv" ! -iname ".*" \) -ls | sort -n
the results do not start ok ("now" is 16:26):
14466561 2636 -rw-r--r-- 1 root root 2695349 Feb 4 16:03 ./20220204T160348.526-cam4-791-AREASbw-32358.jpg
14466569 2208 -rw-r--r-- 1 root root 2257356 Feb 4 16:03 ./20220204T160354.069-cam4-614.jpg
14466571 1044 -rw-r--r-- 1 root root 1068758 Feb 4 16:03 ./20220204T160354.069-cam4-614__chng_182.jpg
14466579 2208 -rw-r--r-- 1 root root 2258697 Feb 4 16:03 ./20220204T160359.296-cam4-352.jpg
14466581 1048 -rw-r--r-- 1 root root 1070111 Feb 4 16:03 ./20220204T160359.296-cam4-352__chng_175.jpg
14466589 2216 -rw-r--r-- 1 root root 2268675 Feb 4 16:04 ./20220204T160404.682-cam4-179.jpg
14466591 1048 -rw-r--r-- 1 root root 1070053 Feb 4 16:04 ./20220204T160404.682-cam4-179__chng_273.jpg
14466597 2204 -rw-r--r-- 1 root root 2253706 Feb 4 16:04 ./20220204T160409.957-cam4-571.jpg
14466599 1048 -rw-r--r-- 1 root root 1070335 Feb 4 16:04 ./20220204T160409.957-cam4-571__chng_154.jpg
14466600 1044 -rw-r--r-- 1 root root 1068675 Feb 4 16:04 ./20220204T160409.957-cam4-571__chng_309.jpg
14466601 2652 -rw-r--r-- 1 root root 2712638 Feb 4 16:04 ./20220204T160409.957-cam4-571-AREASbw-31696.jpg
14466610 2208 -rw-r--r-- 1 root root 2259408 Feb 4 16:04 ./20220204T160415.421-cam4-959.jpg
14466611 1048 -rw-r--r-- 1 root root 1070365 Feb 4 16:04 ./20220204T160415.421-cam4-959__chng_272.jpg
14466620 2212 -rw-r--r-- 1 root root 2264606 Feb 4 16:04 ./20220204T160420.666-cam4-742.jpg
14466621 1048 -rw-r--r-- 1 root root 1070276 Feb 4 16:04 ./20220204T160420.666-cam4-742__chng_173.jpg
and when I continue to scroll up, it arrives to a part where the files are wrongly sorted (like what supposed to be the start of the list):
14457644 2240 -rw-r--r-- 1 root root 2293209 Feb 4 16:26 ./20220204T162633.985-cam4-188.jpg
14457645 1056 -rw-r--r-- 1 root root 1080805 Feb 4 16:26 ./20220204T162633.985-cam4-188__chng_171.jpg
14457658 2236 -rw-r--r-- 1 root root 2286664 Feb 4 16:26 ./20220204T162639.299-cam4-801.jpg
14457659 1056 -rw-r--r-- 1 root root 1080042 Feb 4 16:26 ./20220204T162639.299-cam4-801__chng_158.jpg
14462048 2200 -rw-r--r-- 1 root root 2252735 Feb 4 15:49 ./20220204T154927.284-cam4-095.jpg
14462049 1044 -rw-r--r-- 1 root root 1065249 Feb 4 15:49 ./20220204T154927.284-cam4-095__chng_134.jpg
14462088 2204 -rw-r--r-- 1 root root 2255657 Feb 4 15:49 ./20220204T154932.490-cam4-571.jpg
14462089 1044 -rw-r--r-- 1 root root 1066449 Feb 4 15:49 ./20220204T154932.490-cam4-571__chng_228.jpg
14462118 2204 -rw-r--r-- 1 root root 2254481 Feb 4 15:49 ./20220204T154937.767-cam4-237.jpg
14462127 1044 -rw-r--r-- 1 root root 1066700 Feb 4 15:49 ./20220204T154937.767-cam4-237__chng_79.jpg
How come in the middle it goes back from 15:49 to 16:26?
It happens on every query I'm running, unless it is on a short term like 10 min.
By default sort uses the first column. Use -k to specify column number to sort by.
find . -cmin -50 \( ! -iname "*.csv" ! -iname ".*" \) -ls | sort -n -k 7

Using indirect variable references in a Bash script

I have lots of old log files in a directory, with apps logs generated every month to this directory. I want to use a script to remove only the last month old logs.
This is my file logsDir , these files are 0kb, i have created for question understanding
[root#dasari9 logDir]# ls -ltr
total 136
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram9.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram8.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram7.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram6.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram69.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram68.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram67.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram66.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram5.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram59.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram58.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram57.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram56.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram4.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram49.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram48.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram47.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram46.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram3.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram39.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram38.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram37.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram36.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram2.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram29.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram28.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram27.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram26.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram1.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram19.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram18.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram17.logs
-rw-r--r-- 1 root root 0 Mar 20 12:22 ram16.logs
This is my logs.sh:
#!/bin/bash
Jan=Dec Feb=Jan Mar=Feb Apr=Mar May=Apr Jun=May Jul=Jun Aug=Jul Sep=Aug Oct=Sep Nov=Oct Dev=Nov
DIR=/logDir
cd $DIR
if [ $DIR -eq $PWD ]; then
PWD=$(pwd)
cmd=$(date |awk '{print $2}')
dat=$(ls -ltr |grep $cmd |awk '{print $9}')
rm -rf $dat
else
echo " enterend in the wrong dir"
fi
but it's failing to get that variable value Apr=Mar
This is the output, I'm trying to delete Apr files
[root#dasari9 /]# bash -x log.sh
+ Jan=Dec
+ Feb=Jan
+ Mar=Feb
+ Apr=Mar
+ May=Apr
+ Jun=May
+ Jul=Jun
+ Aug=Jul
+ Sep=Aug
+ Oct=Sep
+ Nov=Oct
+ Dev=Nov
+ DIR=/logDir
+ cd /logDir
++ pwd
+ PWD=/logDir
+ '[' /logDir == /logDir ']'
++ date
++ awk '{print $2}'
+ cmd=Apr
++ ls -ltr
++ grep '$Apr'
++ awk '{print $9}'
+ dat=
+ rm -rf
It seems like you're looking for an indirect variable reference. Your $cmd variable points to the variable $Apr and you want to use the value of $Apr.
Here's how you can do this (just the relevant lines):
cmd=$(date | awk '{print $2}')
eval month=\$$cmd
dat=$(ls -ltr | grep $month | awk '{print $9}')
The eval command resolves the indirect reference created by \$$cmd.
More details here: http://www.tldp.org/LDP/abs/html/ivr.html

combine perl command inside the bash script

Perl is excellent language to store command output to variable
As I know bash isn’t support that
for example
my $value = qx(ls -ltr);
print $value;
it will print: ( example )
-r-xr-xr-x 1 root root 761 May 30 2014 ASon.conf
-r-xr-xr-x 1 root root 699 May 30 2014 ascpl
drwxr-xr-x 2 root root 4096 Feb 24 13:22 da
drwx------ 2 root root 4096 Feb 25 08:08 vm-root
drwxr-x--- 2 root root 4096 Feb 25 08:10 hspeoot
-rw-r--r-- 1 root root 2451 Feb 25 16:12 gry.txt
-rw-r--r-- 1 root root 3112 Mar 1 09:49 new.xml
-rw-r--r-- 1 root root 44893 Mar 1 12:13 SMports.xml
-rwxr-xr-x 1 root root 101 Mar 2 09:56 test
-rwxr-xr-x 1 root root 461340404 Mar 2 10:20 audin.log
the problem is that I write my code with bash
and I want to use the Perl add value in my bash script
so is it possible to combine the following line in my bash script?
my $value = qx(ls -ltr);
in way that when I print the value variable it will print the ls -ltr output
You don't need perl for this, your shell can handle it:
value=$(ls -ltr)
echo "$value"
The quotes when you echo are important.
In Perl:
$var = `shell command`;
$var = qx( shell command );
In sh:
var=`shell command`
var=$( shell command )

error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file:

I'm using libevent for a program, I compile my program on two computers A and B, on A the execution is successful, but on B I got the following errors:
error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
FYI:
A and B are with fedora system, on both A and B, I downloaded libevent-1.4.14b-stable.tar.gz, and install it through configure/make/make install.
on A
[esolve#kitty temp]$ uname -r
3.6.10-2.fc16.x86_64
[esolve#kitty temp]$ ls -al /usr/local/lib64 | grep libevent
[esolve#kitty temp]$ ls -al /usr/lib64 | grep libevent
[esolve#kitty temp]$ ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 32 May 21 00:25 libevent-1.4.so.2 -> /usr/local/lib/libevent-1.4.so.2
[esolve#kitty temp]$ ls -al /usr/local/lib | grep libevent
lrwxrwxrwx 1 root root 21 May 21 00:22 libevent-1.4.so.2 -> libevent-1.4.so.2.2.0
-rwxr-xr-x 1 root root 435304 May 21 00:22 libevent-1.4.so.2.2.0
-rw-r--r-- 1 root root 793178 May 21 00:22 libevent.a
lrwxrwxrwx 1 root root 26 May 21 00:22 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.2.0
-rwxr-xr-x 1 root root 160352 May 21 00:22 libevent_core-1.4.so.2.2.0
-rw-r--r-- 1 root root 269580 May 21 00:22 libevent_core.a
-rwxr-xr-x 1 root root 988 May 21 00:22 libevent_core.la
lrwxrwxrwx 1 root root 26 May 21 00:22 libevent_core.so -> libevent_core-1.4.so.2.2.0
lrwxrwxrwx 1 root root 27 May 21 00:22 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.2.0
-rwxr-xr-x 1 root root 342122 May 21 00:22 libevent_extra-1.4.so.2.2.0
-rw-r--r-- 1 root root 631804 May 21 00:22 libevent_extra.a
-rwxr-xr-x 1 root root 995 May 21 00:22 libevent_extra.la
lrwxrwxrwx 1 root root 27 May 21 00:22 libevent_extra.so -> libevent_extra-1.4.so.2.2.0
-rwxr-xr-x 1 root root 953 May 21 00:22 libevent.la
lrwxrwxrwx 1 root root 21 May 21 00:22 libevent.so -> libevent-1.4.so.2.2.0
on B:
root#kitch server_agent]# uname -r
3.8.5-201.fc18.x86_64
[root#kitch server_agent]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 32 Jun 14 17:09 libevent-1.4.so.2 -> /usr/local/lib/libevent-1.4.so.2
[root#kitch server_agent]# ls -al /usr/local/lib | grep libevent
lrwxrwxrwx 1 root root 21 Jun 14 16:03 libevent-1.4.so.2 -> libevent-1.4.so.2.2.0
-rwxr-xr-x 1 root root 435524 Jun 14 16:03 libevent-1.4.so.2.2.0
-rw-r--r-- 1 root root 805650 Jun 14 16:03 libevent.a
lrwxrwxrwx 1 root root 26 Jun 14 16:03 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.2.0
-rwxr-xr-x 1 root root 164070 Jun 14 16:03 libevent_core-1.4.so.2.2.0
-rw-r--r-- 1 root root 270748 Jun 14 16:03 libevent_core.a
-rwxr-xr-x 1 root root 994 Jun 14 16:03 libevent_core.la
lrwxrwxrwx 1 root root 26 Jun 14 16:03 libevent_core.so -> libevent_core-1.4.so.2.2.0
lrwxrwxrwx 1 root root 27 Jun 14 16:03 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.2.0
-rwxr-xr-x 1 root root 339502 Jun 14 16:03 libevent_extra-1.4.so.2.2.0
-rw-r--r-- 1 root root 642916 Jun 14 16:03 libevent_extra.a
-rwxr-xr-x 1 root root 1001 Jun 14 16:03 libevent_extra.la
lrwxrwxrwx 1 root root 27 Jun 14 16:03 libevent_extra.so -> libevent_extra-1.4.so.2.2.0
-rwxr-xr-x 1 root root 959 Jun 14 16:03 libevent.la
lrwxrwxrwx 1 root root 21 Jun 14 16:03 libevent.so -> libevent-1.4.so.2.2.0
[root#kitch server_agent]# ls -al /usr/lib64 | grep libevent
[root#kitch server_agent]# ls -al /usr/local/lib64 | grep libevent
why my program get this error on B and are there any solutions?
thanks!
Your "A" system has a symlink from /usr/lib/libevent-1.4.so.2 to /usr/local/lib/libevent-1.4.so.2 but system "B" does not. Chances are, if you create it then everything will start working.
The command should be as follows.
sudo ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2
As an alternative, you can add /usr/local/lib to your library path as a test by exporting LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib as a temporary fix on system "B".

Run shell script for every file in directory

I have a bunch of files in a directory all named YYYY_MM_DD
-rw-r--r-- 1 root root 480K Apr 21 13:17 2012_04_05
-rw-r--r-- 1 root root 483K Apr 21 13:17 2012_04_06
-rw-r--r-- 1 root root 484K Apr 21 13:17 2012_04_07
-rw-r--r-- 1 root root 480K Apr 21 13:17 2012_04_08
-rw-r--r-- 1 root root 344K Apr 21 13:17 2012_04_09
-rw-r--r-- 1 root root 66K Apr 21 13:17 2012_04_10
-rw-r--r-- 1 root root 461K Apr 21 13:17 2012_04_11
-rw-r--r-- 1 root root 475K Apr 21 15:09 2012_04_17
-rw-r--r-- 1 root root 480K Apr 21 15:10 2012_04_18
-rw-r--r-- 1 root root 474K Apr 21 15:10 2012_04_19
-rw-r--r-- 1 root root 474K Apr 21 15:10 2012_04_20
I have a shell script that accepts a file as a paramater and calculates figures based on the data in the file, i call the script like this
sh Calculate.sh MyFile
I want to run this shell script for every file in this directory.
How would i go about doing this, xargs ??
Have you tried the find command with execution ?
My sample will echo the files, but you can call a shell script with the filename as a parameter
find . -maxdepth 1 -type f -exec echo {} \;
A simple for loop in the shell:
for file in *; do sh Calculate.sh "$file"; done
find . -maxdepth 1 -type f | xargs -n 1 -I % Calculate.sh %
./Calculate.sh 2012_04_{05..20}

Resources