Retrieve Files along with last modified date under a directory using shell - shell

I am trying to retrieve list of files inside a (unix) directory , I want to retrieve the files along with modified date
ls -LR retrieve the files for me , but I need the modified date alone with it how do I do it ?

Using tree
tree -D
Quoting from man tree
-D Print the date of the last modification time or if -c is used, the last status change time for the file listed.
Test
% tree -D
.
|-- [Oct 19 20:20] dir1
| |-- [Oct 19 19:49] file1
| |-- [Oct 19 19:49] file2
| `-- [Oct 19 19:49] file3
`-- [Oct 19 20:20] dir2
|-- [Oct 19 20:20] file1
|-- [Oct 19 20:20] file2
`-- [Oct 19 20:20] file3
2 directories, 6 files
Using ls followed by awk
ls -lR | awk '$9 {print $6, $7, $8, $9}'
Test
% ls -lR | awk '$9 {print $6, $7, $8, $9}'
Oct 19 19:49 file1
Oct 19 19:49 file2
Oct 19 19:49 file3

Use find -type f -printf "<whatever>" to print the file name plus modification date or anything else you want to know about the file. man find.

Related

grep -l does not behave as expected on piping to xargs [duplicate]

This question already has answers here:
How to ignore xargs commands if stdin input is empty?
(7 answers)
Closed 4 years ago.
So I have a command like: grep "\"tool\":\"SEETEST\"" * -l Which works great standalone - it prints out a list of JSON files generated for the selected tool in the current directory.
But then, if I were to pipe it to xargs ls like that:
grep "\"tool\":\"SEETEST\"" * -l | xargs ls -lSh
It prints all the files in the current directory!
How do I make it print just the matched filenames and pipe them to ls sorted by size?
If there are not matches for xargs, then it will list all files in the current directory:
#----------- current files in the directory
mortiz#florida:~/Documents/projects/bash/test$ ls -ltr
total 8
-rw-r--r-- 1 mortiz mortiz 585 Jun 18 12:13 json.example2
-rw-r--r-- 1 mortiz mortiz 574 Jun 18 12:14 json.example
#----------- using your command
mortiz#florida:~/Documents/projects/bash/test$ grep "\"title\": \"example\"" * -l
json.example
#-----------adding xargs to the previous command
mortiz#florida:~/Documents/projects/bash/test$ grep "\"title\": \"example\"" * -l | xargs ls -lSh
-rw-r--r-- 1 mortiz mortiz 574 Jun 18 12:14 json.example
#-----------adding purposely an error on "title"
mortiz#florida:~/Documents/projects/bash/test$ grep "\"titleo\": \"example\"" * -l | xargs ls -lSh
total 8.0K
-rw-r--r-- 1 mortiz mortiz 585 Jun 18 12:13 json.example2
-rw-r--r-- 1 mortiz mortiz 574 Jun 18 12:14 json.example
If you want to use xargs and grep didn't return any match, then add "-r | --no-run-if-empty" that will prevent xargs to list all the files in the current directory:
grep "\"titleo\": \"example\"" * -l | xargs -r ls -lSh

Csh - Fetching fields via awk inside xargs

I'm struggling to understand this behavior:
Script behavior: read a file (containing dates); print a list of files in a multi-level directory tree and get their size, print the file size only, (future step: sum the overall file size).
Starting script:
cat dates | xargs -I {} sh -c "echo '{}: '; du -d 2 "/folder/" | grep {} | head"
2000-03:
1000 /folder/2000-03balbasldas
2000-04:
12300 /folder/2000-04asdwqdas
[and so on]
But when I try to filter via awk on the first field, I still get the whole line
cat dates | xargs -I {} sh -c "echo '{}: '; du -d 2 "/folder/" | grep {} | awk '{print $1}'"
2000-03:
1000 /folder/2000-03balbasldas
2000-04:
12300 /folder/2000-04asdwqdas
I've already approached it via divide-et-impera, and the following command works just fine:
du -d 2 "/folder/" | grep '2000-03' | awk '{print $1}'
1000
I'm afraid that I'm missing something very trivial, but I haven't found anything so far.
Any idea? Thanks!
Input: directory containing folders named YYYY-MM-random_data and a file containing strings:
ls -l
drwxr-xr-x 2 user staff 68 Apr 24 11:21 2000-03-blablabla
drwxr-xr-x 2 user staff 68 Apr 24 11:21 2000-04-blablabla
drwxr-xr-x 2 user staff 68 Apr 24 11:21 2000-05-blablabla
drwxr-xr-x 2 user staff 68 Apr 24 11:21 2000-06-blablabla
drwxr-xr-x 2 user staff 68 Apr 24 11:21 2000-06-blablablb
drwxr-xr-x 2 user staff 68 Apr 24 11:21 2000-06-blablablc
[...]
cat dates
2000-03
2000-04
2000-05
[...]
Expected output: sum of the disk space occupied by all the files contained in the folder whose name include the string in the file dates
2000-03: 1000
2000-04: 2123
2000-05: 1222112
[...]
======
But in particular, I'm interested in why awk is not able to fetch the column $1 I asked it to.
Ok it seems I found the answer myself after a lot of research :D
I'll post it here, hoping that it will help somebody else out.
https://unix.stackexchange.com/questions/282503/right-syntax-for-awk-usage-in-combination-with-other-command-inside-xargs-sh-c
The trick was to escape the $ sign.
cat dates | xargs -I {} sh -c "echo '{}: '; du -d 2 "/folder/" | grep {} | awk '{print \$1}'"
Using GNU Parallel it looks like this:
parallel --tag "eval du -s folder/{}* | perl -ne '"'$s+=$_ ; END {print "$s\n"}'"'" :::: dates
--tag prepends the line with the date.
{} is replaced with the date.
eval du -s folder/{}* finds all the dirs starting with the date and gives the total du from those dirs.
perl -ne '$s+=$_ ; END {print "$s\n"}' sums up the output from du
Finally there is bit of quoting trickery to get it quoted correctly.

pick up files based on dates in ksh script

I have this list of files . Now I will have to pick the latest file based on some condition
3679 Jul 21 23:59 belk_rpo_error_**po9324892**_07212014.log
0 Jul 22 23:59 belk_rpo_error_**po9324892**_07222014.log
3679 Jul 23 23:59 belk_rpo_error_**po9324892**_07232014.log
22 Jul 22 06:30 belk_rpo_error_**po9324267**_07012014.log
0 Jul 20 05:50 belk_rpo_error_**po9999992**_07202014.log
411 Jul 21 06:30 belk_rpo_error_**po9999992**_07212014.log
742 Jul 21 07:30 belk_rpo_error_**po9999991**_07212014.log
0 Jul 23 2014 belk_rpo_error_**po9999991**_07232014.log
For a PATRICULAR Order_No(Marked with ** **)
If the latest file is 0 kB then we will discard it (rest of the files with same Order_no as well)
if the latest file is non Zero then I will take it.(Only the latest one)
Then append the contents in a txt file .
My expected output would be ::
411 Jul 21 06:30 belk_rpo_error_**po9999992**_07212014.log
3679 Jul 23 23:59 belk_rpo_error_**po9324892**_07232014.log
22 Jul 22 06:30 belk_rpo_error_**po9324267**_07012014.log
I am at my wits end here. I cant seem to figure out how to compare dates in Unix. Any help is very appreciated.
You can try something like:
touch test.txt
for var in ` find . ! -empty -exec ls -r {} \;`
do
cat $var>>test.txt
done
untested
use stat to emit date (epoch time), size and filename.
use awk to filter out zero-length files and extract order number.
sort by order number and date
awk to pick up the last filename for each order number
stat -c $'%Y\t%s\t%n' *.log |
awk -F'\t' -v OFS='\t' '
$2 > 0 {
split($3, a, /_/)
print a[4], $1, $3
}' |
sort -t $'\t' -k1,1 -k2,2n |
awk -F'\t' '
NR > 1 && $1 != prev_order {print filename}
{filename = $3; prev_order = $1}
END {print filename}
'
The sort command might be wrong: In order to group by order number, you might need to sort first by file time then by order number.
If I understand your question, the resulting files need to be concatenated and appended to a file. If the above pipeline is working OK, then pipe into | xargs cat >> something.log

find files with same names in directory

I was wondering in case I could do the following, better than what I have.
Objective: Identify files with the same name in the directory tree. I do not have any knowledge if there would be any duplicate file or the location/name of such files.
Expected output: List the files with the location.
Input provided: path of top directory for search.
My algorithm:
1.list all file in the target directory (I have used find -name ".")
2.List1: sort the file names
3.List2: Uniquify files names
4.Diff lists from step 2 & 3 to get the repeated file
5.extract the location.
Sample Directory:
temp/
|-- d1
| |-- d2
| | `-- f3
| |-- d3
| | `-- f3
| |-- f1
| `-- f2
`-- d4
|-- d5
| |-- f2
| `-- f6
|-- f4
`-- f5
> find temp/ -type f -follow -print | sed 's;.*/;;' | sort -u > ~/tmp/12
> find temp/ -type f -follow -print | sed 's;.*/;;' | sort -n > ~/tmp/11
> diff ~/tmp/11 ~/tmp/12
3,4d2
< f2
< f3
> find temp/ -name f2
temp/d1/f2
temp/d4/d5/f2
> find temp/ -name f3
temp/d1/d2/f3
temp/d1/d3/f3
I want to simplify this process. Any help would be appriciated. Please let me know in case you need further details.
Guys this is a solution that I identified fitting my needs and may help you:
Your comments are welcome.
set idirectory = `echo $* | awk '{print $1}'`
if ( -d $idirectory ) then
foreach xxx (`find $idirectory -type f -follow -print | sed 's;.*/;;' | sort -n | uniq -d`)
echo "Multiple files found for " $xxx
find $idirectory -name $xxx
end
endif

Folders tree with rights

In OS X and SunOS OS there is no exist the 'bash tree command'.
To plot a tree "graph" of folders i use the following instruction:
find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
Or this to show files too.
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
But i need another version which contains also the folders rights. Im pretty lost to add on the right side the folder rights. Anyone have any idea ??
Update:
There are any option to plot the files inside the folders and their rights too. I'm trying with this command find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g' and doing combination with the solution provided by #fedorqui but the result aren't so good.
That's the result obtained with the above command, without rights.
| | |____src
| | | |____cft2exit.c
| | | |____cft2exit_AIX
| | | |____cft2exit_SUN
| | | |____gestidt.c
| | | |____gestidt.h
| | | |____gestidt.o
| | | |____gestidt_AIX
| | | |____gestidt_SUN
| | | |____gestidt_SunOS
| | | |____makefile
| | | |____sem.a
| | | |____ut_sem.c
| | | |____ut_sem.h
| | | |____ut_sem.o
| |____data
| | |____purge.dat
| |____lost+found
You can execute ls -ld for each result of find. It will give you the permissions, other things, and then the file name. If you then pipe to awk, with awk '{print $NF, $1}' you can print both blocks of information. Finally, you pipe to your sed command. All together:
find . -type d -exec ls -ld {} \; | awk '{print $NF, $1}' | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
Test
$ find . -type d -exec ls -ld {} \; | awk '{print $NF, $1}' | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
. drwxrwxr-x
|____python drwxrwxr-x
| |____jinja2 drwxrwxr-x
| | |____bk drwxrwxr-x
| | |____infiles drwxrwxr-x
.......
In small steps:
$ find . -type d -exec ls -ld {} \;
drwxrwxr-x 7 me me 4096 Aug 15 15:35 .
drwxrwxr-x 3 me me 4096 Aug 13 14:31 ./python
drwxrwxr-x 4 me me 4096 Apr 26 15:14 ./python/jinja2
drwxrwxr-x 2 me me 4096 Apr 19 14:26 ./python/jinja2/bk
drwxrwxr-x 2 me me 4096 Apr 19 12:54 ./python/jinja2/infiles
and then
$ find . -type d -exec ls -ld {} \; | awk '{print $NF, $1}'
. drwxrwxr-x
./python drwxrwxr-x
./python/jinja2 drwxrwxr-x
./python/jinja2/bk drwxrwxr-x
./python/jinja2/infiles drwxrwxr-x
On OS X you can install tree, using homebrew:
brew install tree
or, using macports:
sudo port install tree
and then, to view directories with permissions:
$ tree -p -d
Sample output:
.
├── [drwxr-xr-x] folder1
│   └── [drwxr-xr-x] folder2
│   └── [drwxr-xr-x] folder3
│   └── [drwxr-xr-x] folder4
└── [drwxr-xr-x] folder5
├── [drwxr-xr-x] folder6
└── [drwxr-xr-x] folder7

Resources