1846629 Members
1730 Online
110256 Solutions
New Discussion

Re: list file problem

 
ust3
Regular Advisor

list file problem

When I use find . -type f -exec ls -lt {} \; to list file , the output is as below , all have ./ sign in every fle , can advise how to erase the ./ of the output ? Thx

./test1.txt
./test2.txt
./test3.txt

my desired output is
test1.txt
test2.txt
test3.txt

13 REPLIES 13
Robert-Jan Goossens
Honored Contributor

Re: list file problem

Hi,

Try,

# find . -type f -exec ls {} \; | awk -F/ '{ print $2 }'

# find . -type f | xargs ls | awk -F/ '{ print $2}'

Robert-Jan
Shrikant Lavhate
Esteemed Contributor

Re: list file problem

Try,

#find . -type f -exec ls {} \; | cut -c 3-

Best Luck.
Will it remain a personal, if I broadcast it here!
Dennis Handly
Acclaimed Contributor

Re: list file problem

Robert-Jan: ... awk -F/

It is probably safer to remove the "./" then use awk -F/:
$ find . -type f -exec ll -t + | sed 's:\./::'

Especially if find goes into subdirectories.
Matti_Kurkela
Honored Contributor

Re: list file problem

For example:
$ find . -type f -exec ls -lt {} \;
-rw-r--r-- 1 user group 0 Dec 14 11:46 ./testdir/test4.txt
-rw-r--r-- 1 user group 0 Dec 14 11:46 ./test1.txt
-rw-r--r-- 1 user group 0 Dec 14 11:46 ./test2.txt
-rw-r--r-- 1 user group 0 Dec 14 11:46 ./test3.txt


$ find . -type f -exec ls -lt {} \; | sed -e 's! ./! !'
-rw-r--r-- 1 user group 0 Dec 14 11:46 testdir/test4.txt
-rw-r--r-- 1 user group 0 Dec 14 11:46 test1.txt
-rw-r--r-- 1 user group 0 Dec 14 11:46 test2.txt
-rw-r--r-- 1 user group 0 Dec 14 11:46 test3.txt

Note that "-exec {} \;" syntax is very slow if there are lots of files, because it forks a new process for each file. As the ls command gets only one file to list at a time, the -t option (sort by time) will not be effective: the listing will be in the order determined by the find command.

The "-exec \+" syntax will be much more efficient, as each ls command will get as many files to list as possible (the limitation is the maximum length of a command line). If you use the -t option with the ls command and the list of files fits on a single command line, your list is guaranteed to be correctly ordered by time; but if find needs to execute multiple "ls -lt" commands, the sorting order of the entire list cannot be guaranteed.

$ find . -type f -exec ls -lt \+ | sed -e 's! ./! !'
-rw-r--r-- 1 mkurkela mkurkela 0 Dec 14 11:46 testdir/test4.txt
-rw-r--r-- 1 mkurkela mkurkela 0 Dec 14 11:46 test3.txt
-rw-r--r-- 1 mkurkela mkurkela 0 Dec 14 11:46 test2.txt
-rw-r--r-- 1 mkurkela mkurkela 0 Dec 14 11:46 test1.txt

MK
MK
ust3
Regular Advisor

Re: list file problem

thx reply and sorry to mistake

my desired output should be as below , can advise how to do it ? thx

-rw-r--r-- 1 user1 edp 1296 Jun 20 07:11 test1.txt
-rw-r--r-- 1 user1 edp 1296 Jun 20 07:11 test2.txt
-rw-r--r-- 1 user1 edp 1296 Jun 20 07:11 test3.txt
Dennis Handly
Acclaimed Contributor

Re: list file problem

>my desired output should be as below

My sed will do that. But as MK says, it may not be sorted the way you want.
Shrikant Lavhate
Esteemed Contributor

Re: list file problem

Already answered by Matti usind stream editior sed.!! check above..
Will it remain a personal, if I broadcast it here!
Dennis Handly
Acclaimed Contributor

Re: list file problem

> Shrikant: Already answered by Matti using sed.

Actually MK forgot to quote the ".". But he did guard for embedded ".". by using a space, so:
... | sed 's: \./: :'
Sandman!
Honored Contributor

Re: list file problem

How about the output of pwd(1) to find(1) instead of using "." in the path-list?

# find `pwd` -type f -exec ll -lt {} \;
Dennis Handly
Acclaimed Contributor

Re: list file problem

>Sandman: How about the output of pwd(1)

Using $PWD in find will give absolute paths for everything.
$ find $PWD -type f -exec ll +
Arturo Galbiati
Esteemed Contributor

Re: list file problem

Hi,
why not only:
ls -lt|grep -v ^d

You will have a list of file in cronological order in current directory but not directories

HTH,
Art
ust3
Regular Advisor

Re: list file problem

thx reply ,

I tried all command above , but the output file name still have ./ as below


Now the output
==============
-rw-r--r-- 1 user1 edp 1296 Jun 20 07:11 ./test1.txt
-rw-r--r-- 1 user1 edp 1296 Jun 20 07:11 ./test2.txt
-rw-r--r-- 1 user1 edp 1296 Jun 20 07:11 ./test3.txt


My desired output
=================
-rw-r--r-- 1 user1 edp 1296 Jun 20 07:11 test1.txt
-rw-r--r-- 1 user1 edp 1296 Jun 20 07:11 test2.txt
-rw-r--r-- 1 user1 edp 1296 Jun 20 07:11 test3.txt


can advise what can i do ? thx
Dennis Handly
Acclaimed Contributor

Re: list file problem

>but the output file name still have ./ as below

I thought I mentioned why on reply with 11:12:26 GMT. Try this:
$ find . -type f -exec ll -t + | sed 's: \./: :'