how to let wc command recursivly? [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Let it counting *.h *.cpp in Sub directory.

If you want it seperate per file:
find -type f \( -name "*.h" -o -name "*.cpp" \) -exec wc {} \;
if you want the accumulated sum:
find -type f \( -name "*.h" -o -name "*.cpp" \) -exec cat {} \; | wc -l

bash 4
shopt -s globstar
wc **/*.{cpp,h}

I think find and xargs is clearer and easier to work with instead of find -exec but it's style choice.
find . -name "*.h" -or -name "*.cpp" | xargs wc

Use zsh instead of bash:
wc **/*.(cpp|h)
This will expand out to all the .cpp and .h files in the current directory and all subdirectories.

Related

how to compare contents and mode for 2 linux folders? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
Given 2 folders in centos : /folder1 and /folder2 and each folder has some files and subfolders inside.
I use beyond compare 3 to compare the contents but don't know how to compare the file mode and owner at the same time .
Thanks for any help!
If it didn't have to be done all at once, you could first diff <(cd /folder1; ls -lR) <(cd /folder2; ls -lR) | grep '^[<>]' to get owner/mode differences, and then diff -r /folder1 /folder2 to get content differences.
If you really want it to be done all at once, you could generate a list for each directory that includes name, owner, mode, and checksum, and compare the two. This will only tell you which files are different, not what the changes in them are, though.
diff \
<(find /folder1 -printf '%P\t%u:%g\t%M' \( \
-type b -exec stat -c '\tb:%t:%T\n' -- '{}' \; -o \
-type c -exec stat -c '\tc:%t:%T\n' -- '{}' \; -o \
-type d -printf '/\n' -o \
-type p -printf '|\n' -o \
-type f -printf '\t' -exec sum -- '{}' \; -o \
-type l -printf '\t-> %l\n' -o \
-type s -printf '=\n' -o \
-printf '\t???\n' \) | sort) \
<(find /folder2 -printf '%P\t%u:%g\t%M' \( \
-type b -exec stat -c '\tb:%t:%T\n' -- '{}' \; -o \
-type c -exec stat -c '\tc:%t:%T\n' -- '{}' \; -o \
-type d -printf '/\n' -o \
-type p -printf '|\n' -o \
-type f -printf '\t' -exec sum -- '{}' \; -o \
-type l -printf '\t-> %l\n' -o \
-type s -printf '=\n' -o \
-printf '\t???\n' \) | sort) | \
grep '^[<>]'

Exclude a subpath in find command [duplicate]

This question already has answers here:
How do I exclude a directory when using `find`?
(46 answers)
Closed 7 years ago.
I have the following find command:
find /mnt/F_11 -type f \( -iname '*.xls' -o -iname '*.xlsx' /)
How would I find all items in /mnt/F_11 but not in /mnt/f_11/DONOTENTER/?
In other words, I would want it to search:
YES /mnt/F_11
YES /mnt/F_11/somepath/
YES /mnt/F_11/somepath/other/
NO /mtn/F_11/DONOTENTER/
Use -prune to avoid recursing down branches you don't want to follow.
find /mnt/F_11 -name DONOTENTER -prune -o \
-type f \( -iname '*.xls' -o -iname '*.xlsx' \) -print
Note the explicit -print at the end -- this is important, as otherwise the implicit print action covers both branches.

Linux find with prune and negation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to grep all files in a directory except for
subdirectories of lib
images (png and jpg)
I'm doing it in a shell script, passing the arguments to grep, no problem.
This command excludes the subdirectories of lib
find src \
-name lib -prune -o \
-type f -exec grep -P "$#" {} +
and this one excludes the images
find src \
! -name "*.jpg" ! -name ".png" \
-type f -exec grep -P "$#" {} +
Put together as
find src \
-name lib -prune -o \
! -name "*.jpg" ! -name ".png" \
-type f -exec grep -P "$#" {} +
it fails to exclude the images. Any idea what's going on?
It fails to exclude png images because you left out the * in -name "*.png".
A generally useful approach is to filter results via grep after a pipe, this decreases complexity in the original command, so:
find [simplified find options] | egrep -v ".jpg|.png"

Deleting large number of files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I'm writing a script to delete large number of files in linux. The script will run every two minutes in crontab. I tried using the
find /mnt/md0/capture/DCN/ -maxdepth 1 -type f -name "*.pcap" -print0 | xargs -0 ls -lt | tail -$del | awk '{print $8}'
and saving this to a variable and tried to delete using rm command, but dint run. I have found out that find . -type f -delete can be used to delete the files, but I don't know how to add this part to my command. How would I do this?
Just replace -print0 with -delete:
find /mnt/md0/capture/DCN/ -maxdepth 1 -type f -name "*.pcap" -delete

Find in file and then move that file using Linux? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I want to be able to find files that contain certain strings and the move that list of files to directory X
I can use this command to find the files
find . -iname 'commaus*' | xargs grep '>24901<' -sl
and this command to move files
mv * /home/user/scripts/xslt
But is there a way to combine these commands so that the found files are moved.
I have seen similar joined find/action commands such as
find /home/user -name property_images -ok rm -f {} \;
but following this structure is returning an error
find . -iname 'commaus*' | xargs grep '>24901<' -sl -ok mv {} /home/user/scripts/xslt;
Use a loop. In this case, try:
for i in `find . -iname 'commaus*' | xargs grep '>24901<' -sl`; do mv "$i" /home/user/scripts/xslt/; done
Very hackish, but it should work.
you can do this by wrapping it in a for loop
for i in `find /path/to/search -iname 'optionalfilename' -exec grep -H -m1 '>24901<' {} \; | awk -F: '{print $1}'
do
mv $i /path/to/new/location
done
This will not work as expected if filenames contain spaces or colons
Also might be able to try (without loop):
find . -iname 'commaus*' | grep '>24901<' -sl -ok | xargs mv -t /home/user/scripts/xslt

Resources