pipe a command after splitting the returned value - linux

I'm using a find command which results in multiple lines for result, I then want to pipe each of those lines into an ls command with the-l option specified.
find . -maxdepth 2 -type f |<some splitting method> | ls -l
I want to do this in one "command" and avoid writing to a file.

I believe this is what you are looking for:
find . -maxdepth 2 -type f -exec ls -l {} \;
Explanation:
find . -maxdepth 2 -type f: find files with maxdepth at 2
-exec ls -l {} \; For each such result found, run ls -l on it; {} specifies where the results from find would be substituted into.

The typical approach is to use -exec:
find . -maxdepth 2 -type f -exec ls -l {} \;

Sounds like you are looking for xargs. For example, on a typical Linux system:
find . -maxdepth 2 -type f -print0 | xargs -0 -n1 ls -l

Related

Linux find command get all text in the file and print file path

I need to get all the texts in the matching file in the folder. However, at the same time need to get the matching file path as well. How can I get the matching file path as well using the following command.
find . -type f -name release.txt | xargs cat
try
find . -type f -name release.txt -exec grep -il {} \; | xargs cat
Skip xargs, just do:
find . -type f -name release.txt -exec sh -c 'echo "$1"; cat "$1"' _ {} \;

Show only directories, not their contents with `find -type d | xargs ls`

I want to find some folders by name, and then list their information using "ls", here is what i did using "find",
find ./ -mindepth 1 -maxdepth 3 -type d -name logs
what i got is:
./RECHMN32Z/US/logs
./RECHMN32Z/UM/logs
./RECHMP3BL/US/logs
./RECHMP3BL/UM/logs
./RECHMAS86/UM/logs
./RECHMAS86/US/logs
and then i add "xargs ls -l" , then it will return information of all files under these folders returned above,
if i just want to list information of these folders, how to do ?
It's not find or xargs's fault, but ls's. When given directory names ls shows their contents. You can use -d to have it only show the directories themselves.
find has a -ls action that uses the same format as ls -dils. No need to invoke an external command.
find ./ -mindepth 1 -maxdepth 3 -type d -name logs -ls
Or use ls -ld to list the directories and not their contents. -exec cmd {} + is a simpler alternative to xargs. No pipeline required.
find ./ -mindepth 1 -maxdepth 3 -type d -name logs -exec ls -ld {} +

Formatting md5sum differently?

I need to find the md5sum of files recursively and list these files alphabetically. However, in my final output I don't want the sum to actually show up. For example if I issue:
find -not -empty -type f -exec md5sum "{}" \;
I get this:
0df8724ef24b15e54cc9a26e7679bb90 ./doc1.txt
d453430ce039863e242365eecaad7888 ./doc2.txt
53b2e8ae1dfaeb64ce894f75dd6b957c ./test.sh~
1ba03849883277c3c315d5132d10d6f0 ./md5file.txt
6971b4dbbd6b5b8d1eefbadc0ecd1382 ./test.sh
is there a simple way make this command to show only the files like:
./doc1.txt
./doc2.txt
./test.sh~
./md5file.txt
./test.sh
thx!
As Cyrus and Sriharsha say, simply using:
find -not -empty -type f
will give you the result you need.
Pass the output of find command to awk or cut.
find -not -empty -type f -exec md5sum "{}" \; | awk '{print $2}'
OR
Use sed if the filename contains spaces.
find -not -empty -type f -exec md5sum "{}" \; | sed 's/^[^ ]\+ \+//'

Find and exec command in linux. Is it possible to pipe 2 find and exec commands

I'm trying to accomplish this task
1) Find directory A (DIR_A) and copy all files in the directory(including its sub-directory, if any) into a new directory called DIR_B
2) In directory (DIR_B),replace the word apple with orange
I executed the following code and for some reason, it copies all the files but it fails on the second task (replace apple with orange). I would appreciate help on this. Below is my code
find DIR_A -iname FILEA -type f -exec cp {} DIR_B \;|find DIR_B/ -iname \*.* -type f -exec sed -i "s|apple|orange|g" {} \;
Rather than trying to piping the output from one find into the other, why not just run them sequentially? I'm not sure that find reads from its stdin.
find DIR_A -iname FILEA -type f -exec cp {} DIR_B \; ; find DIR_B/ -iname \*.* -type f -exec sed -i "s|apple|orange|g" {} \;
I've replaced your pipe with a semi-colon.
Try this :
Sed Syntax :
sed 's/old/new/g'
find DIR_A -iname FILEA -type f -exec cp {} DIR_B \;|find DIR_B/ -iname \*.* -type f -exec sed -i "s/apple/orange/g" {} \;

Find files older than X and Count them

Using Linux. What I need to do is determine the number of files in a directory(recursively) that are older than DATE and echo that number.
I have:
find /u1/database/prod/arch -type f -mtime +10 -exec ls -laR | wc -l \;
That lists the files fine.
And then I have:
ls -laR | wc -l
Which lets me count the files recursively.
But I can't seem to put them together. I think I need a script to do this but don't know how to do that.
Would love some help
find /u1/database/prod/arch -type f -mtime +10 | wc -l
works here.
You dont need the exec. use -print (or nothing) and find will print a line per file (and handle the recursion)
find /u1/database/prod/arch -type f -mtime +10 -print | wc -l

Resources