Deleting large number of files [closed] - linux

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

Related

Shell Script to compress all the log files in "my_dir" directory after the configurable number of hours or days [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 3 years ago.
Improve this question
I want a shell script to compress all the log files in "my_dir" directory after the configurable number of hours or days.
I have tried with the following script but it didn't work :
find /path/to/directory -mtime +2 -exec ls "{}" \
find /path/to/directory -mtime +2 -exec ls "{}" \;
The script is getting executed but no action is done.
Maybe you just want configure the logrotate on server, see this link: https://www.thegeekstuff.com/2010/07/logrotate-examples
And if you want execute at any time, you can call for an execution with this command (but it's unnecessary if the config file had correctly configurated):
#: logrotate /path/to/my/logRotateFile.conf
You can try this command for hours configuration:
find /path/to/directory/*.log -type f ! -name "*.tar.gz" -mmin +$[$COMPRESS_HOURS * 60] -exec mv '{}'
COMPRESS_HOURS are configurable
And for days
find /path/to/directory/*.log -type f ! -name "*.tar.gz" -mtime +$days -exec mv '{}'
days are configurable

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.

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

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

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.

exclude directories mv unix [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
The command below moves every hidden/normal file ending with *string without . or _ before it.
mv {.,}*[!._]string /destination
How can I also exclude moving all directories in the above command?
Try
find /WHERE/TO/FIND -name '*STRING' \( ! -name '*_STRING' -o ! -name '*.STRING' \) -type f -exec mv \{\} /WHERE/TO/MOVE \;
Note, if you want to move every file from only the /WHERE/TO/FIND directory, you should add -maxdepth 1 (after e.g. the -type f part).
How about:
for file in {.,}*[!._]string; do test -f "$file" && mv "$file" /destination; done
In what shell does the [!._] glob actually work when used with {.,}? You would probably be better off avoiding the {} notation and do:
for file in .*[!._]string *[!._]string; do ... ; done

Resources