LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-23-2013, 06:58 AM   #1
mnt_schred
LQ Newbie
 
Registered: Dec 2009
Distribution: CentOS
Posts: 7

Rep: Reputation: 0
recursively trying to clear content of files


In order to find shared photo's which I lost but others might have, I would like to sent people a zip with my photo's. I first wanted to send a list, but I realized that when I send them directory with subdirectories with emtpy files they can just copy their photo's over my files with 'overwrite none'. Then, after deleting the empty files, I can synchronize their photo's with mine.

I know I can empty files with cat /dev/null > filename
and I know I can list all .jpg files recuresively with .find -type '*.jpg'
but when I execute this command:

find . -name '*.jpg' -exec cat /dev/null > "{}" \;

nothing happens...
what am I doing wrong?
 
Old 11-23-2013, 09:09 AM   #2
mddnix
Member
 
Registered: Mar 2013
Location: Bengaluru, India
Distribution: Redhat, Arch, Ubuntu
Posts: 502

Rep: Reputation: 137Reputation: 137
Interesting question... a quick google got these results:
Code:
find . -name '*.jpg' -exec /bin/bash -c "> '{}'" \;
find . -name '*.jpg' -exec dd if=/dev/null of="{}" \;
 
Old 11-23-2013, 11:11 AM   #3
mnt_schred
LQ Newbie
 
Registered: Dec 2009
Distribution: CentOS
Posts: 7

Original Poster
Rep: Reputation: 0
I still don't get why the cat method wouldn't work though.. But this works!
I've used iname instead to also include files with capitals in the extension and added a few other filetypes; my final command is:

find . \( -iname \*.jpg -o -iname \*.mpg -o -iname \*.mov -o -iname \*.avi -o -iname \*.png \) -exec /bin/bash -c "> '{}'" \;

Thanks!
 
Old 11-23-2013, 12:04 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Quote:
Originally Posted by mnt_schred View Post
I still don't get why the cat method wouldn't work though..
Redirections (">") are handled by the shell, so when you say
Code:
find . -name '*.jpg' -exec cat /dev/null > "{}" \;
The shell does this
Code:
# execute find with the following arguments
find . -name *.jpg -exec cat /dev/null ;
# with output redirected to a file called {}
The arguments between -exec and ; are what find executes; obviously cat /dev/null produces nothing useful so you get an empty file called {}.

To handle the redirection the way you want, you to quote it to protect from the outer shell, and tell find to use a shell to interpret the quoted command:

Code:
find . -name '*.jpg' -exec /bin/bash -c "cat /dev/null > '{}'" \;
which means

Code:
# execute find with the following arguments
find . -name *.jpg -exec /bin/bash -c cat /dev/null > '{}' ;

Although, as you can see from mddesai's suggestion, cat and /dev/null aren't strictly needed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Apache problem: Aliased content isn't transmitted in clear; not SSL. cignul9 Linux - Server 0 11-05-2012 01:24 PM
Recursively clear files carlosmp Programming 13 08-23-2012 05:11 PM
[SOLVED] Apache/Php code to recursively display folder content lpallard Programming 1 12-30-2010 05:03 PM
how to clear a file's content? iclinux Linux - Newbie 7 03-30-2009 03:01 PM


All times are GMT -5. The time now is 06:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration