How to remove zero byte files [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 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

Related

Gzip or tars logs that are older than 7 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 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 2 years ago.
Improve this question
Need to create a single script that would Gzip or tars logs that are older than 7 days in multiple (3) paths /home/temp, home/logs, then confirm this one is over 50% capacity home/var/lib/mongo. This is what I got so far but I can't think of how to combine these:
find . -mtime +7 -print -exec gzip {} \; for all 3 but them
find /tmp/log/ -mtime +7 -type f -exec sh -c \
'tar -czvPf /tmp/older_log_$(basename $0)_$(date +%F).tar.gz $0' {} ;
# create a single tar file for each archive +7 days old on one mount
If I understand you correclty, you would like to archive multiple old logs in one tar files:
find /tmp/log /home/temp /home/logs -mtime +7 -type f \
| xargs tar -czf /tmp/older_log_$(date +%F).tar.gz --remove-files
For easy reading I put it on two lines.
It searches for all files you want to archive
Pass the found files as arguments to the tar archive (using xargs)
tar will make the new archive and add all the files
additionally tar will remove the original files with the last oiption (from GNU tar)

linux pipe argument list too long [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 8 years ago.
Improve this question
I use the following bash script to remove files older than $days.
find /home/xxx/conf_* -maxdepth 0 -mindepth 0 -type d -ctime +5 -exec rm -rf {} \;
However if the files are more than 32000+, I get
/usr/bin/find: Argument list too long
how do I trim the list down to like 20000 only?
From comment to answer:
Your problem is the glob expansion but you are already using a tool that can perfectly well handle an arbitrary number of found results, namely find. As such you should use a glob at all. Instead you should let find do all the work.
Something like:
find /home/xxx -maxdepth 1 -name 'conf_*' -type d -ctime +5 -exec rm -rf {} \;
Also if your find has -exec \+ you should probably use this instead:
find /home/xxx -maxdepth 1 -name 'conf_*' -type d -ctime +5 -exec rm -rf {} \+
For such a large number of matching directories I imagine the significantly reduced amount of executions of rm might be significantly more efficient.

finding files and moving their 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 9 years ago.
Improve this question
I have a huge number of text files, organized in a big folder tree, on Debian Linux. What I need is to find all text files having a specific name pattern and then move the containing folder to a destination.
Example:
/home/spenx/src/a12/a1a22.txt
/home/spenx/src/a12/a1a51.txt
/home/spenx/src/a12/a1b61.txt
/home/spenx/src/a12/a1x71.txt
/home/spenx/src/a167/a1a22.txt
/home/spenx/src/a167/a1a51.txt
/home/spenx/src/a167/a1b61.txt
/home/spenx/src/a167/a1x71.txt
The commands:
find /home/spenx/src -name "a1a2*txt"
mv /home/spenx/src/a12 /home/spenx/dst
mv /home/spenx/src/a167 /home/spenx/dst
The result:
/home/spenx/dst/a12/a1a22.txt
/home/spenx/dst/a167/a1a22.txt
Thank you for your help.
SK
combination of find, dirname and mv along with xargs should solve your problem
find /home/spenx/src -name "a1a2*txt" | xargs -n 1 dirname | xargs -I list mv list /home/spenx/dst/
find will fetch list of files
dirname will extract path of file. Note that it can only take one argument at a time
mv will move source directories to destination
xargs is the key to allow output of one command to be passed as arguments to next command
For details of options used with xargs, refer to its man page of just do man xargs on terminal
You can execute:
find /home/spenx/src name "a1a2*txt" -exec mv {} /home/spenx/dst \;
Font: http://www.cyberciti.biz/tips/howto-linux-unix-find-move-all-mp3-file.html
Create this mv.sh script in the current directory that will contain this:
o=$1
d=$(dirname $o)
mkdir /home/spenx/dst/$d 2>/dev/null
mv $o /home/spenx/dst/$d
Make sure it is executable by this command:
chmod +x mv.sh
Next call this command:
find /home/spenx/src -name "a1a2*txt" -exec ./mv.sh {} \;
find /home/spenx/src -name "a1a2*txt" -exec mv "{}" yourdest_folder \;
There's probably multiple ways to do this, but, since it seems you might have multiple matches in a single directory, I would probably do something along this line:
find /home/spenx/src -name "a1a2*txt" -print0 | xargs -0 -n 1 dirname | sort -u |
while read d
do
mv "${d}" /home/spenx/dst
done
It's kind of long, but the steps are:
Find the list of all matching files (the find part), using -print0 to compensate for any names that have spaces or other odd characters in them
extract the directory part of each file name (the xargs ... dirname part)
sort and uniquify the list to get rid of duplicates
Feed the resulting list into a loop that moves each directory in turn

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