best way to find the number of files in a directory in unix [closed] - linux

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
For counting the number of files in directory i know two methods
first one ls -l file* |wc -l
second one find file* -type f -maxdepth 1 | wc -l
which one is more relibale and correct one?

Prefer the find option, but use -name 'file*' (in single quotes), as in
find . -maxdepth 1 -name 'file*' -type f | wc -l
This will avoid globbing, as both examples above I believe may run into a max args limitation.
`/home/charles/data/Study$ find . -maxdepth 1 -name CL* -type f | wc -l
bash: /usr/bin/find: Argument list too long
`/home/charles/data/Study$ find . -maxdepth 1 -name `CL*` -type f | wc -l
318480
There is no such solution for ls, so find is slightly more dependable. This is all dictated by ARG_MAX, as in:
`/home/charles/data/Study$ getconf ARG_MAX
2097152 # in bytes

Related

linux shell script to copy directory tree and link files [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to be able to create a copy of a directory tree and soft link the files on it.
For example, from
/home/user/origin/a/sub/file.txt
I would like to get
/home/user/destination/a/sub/file.txt
being this one a link to the original file.txt.
I tested with
find /home/user/origin/ -type d -printf "mkdir -vp '/home/user/destination%p'\n" -o -type f -printf "ln -vs '%p' '/home/user/destination%p'\n" | sh
but it has two problems:
I'd like to copy from origin to destination, and it copies from origin to /home/user/destination/home/user/origin. It is not a biggie, as I can move that afterwards
If the file name is something like
In Fifty Years We'll All Be Chicks.txt
It stops working because the '.
Assuming I understand what you're trying to do, it seems easier to just use -exec
find /home/user/origin/ \
-type d -exec sh -c 'mkdir -v "/home/user/destination/${0#/home/user/origin/}"' {} \; \
-o \
-type f -exec sh -c 'ln -vs "$0" "/home/user/destination/${0#/home/user/origin/}"' {} \;
Note -or having lower precedence than the implied -and's is important here.

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

How to remove zero byte files [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 9 years ago.
Improve this question
I have the following directory structure:-
foo/dir1/
foo/dir2/
foo/dir3/
foo/dir1/a.rb
foo/dir1/b.rb
foo/dir1/c.rb
foo/dir1/d.rb
foo/dir2/e.rb
foo/dir2/f.rb
foo/dir2/g.rb
foo/dir2/h.rb
How to remove zero byte files from a certain folder (some of the files under dir1, dir2 are zero bytes). How do I find and remove such files?
Assuming you have a version of find compliant enough with POSIX 2008 to support the + notation:
find foo -size 0 -exec rm -f {} +
If you don't, there are variants you can use:
find foo -size 0 -print0 | xargs -0 rm -f # But you probably have + anyway
find foo -size 0 -exec rm -f {} \; # Slow but reliable
find foo -size 0 -print | xargs rm -f # Fails with spaces etc in file names
And the accepted answer to the duplicate question suggests -delete, which is good when it is supported by the find you are using (because it avoids the overhead of executing the rm command by doing the unlink() call inside find):
find foo -size 0 -delete # Not POSIX standard

chmod exclusions [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 9 years ago.
Improve this question
I know I should assign a group and then set an umask so that groups writable permissions persist but for whatever reason I can't do this. I need to chmod recursively a directory except one sub folder (web10), would the following work?
cd /var/www/clients/
find . -type f -not -path "*web10*" -exec chmod 777 '{}' \;
If you want to exclude files or directories, you use -prune
find /var/www/clients/ -name web10 -type d -prune -o -type f -print0 | xargs -0 chmod 0640
You should also use xargs where possible. With -exec you call the command once for every file found, whereas xargs collects as many files as possible and calls the command once for N files, resulting in a more efficient execution and better performance.

how to let wc command recursivly? [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 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.

Resources