Create a dummy file in all directories that include a .jpg


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a dummy file in all directories that include a .jpg
# 1  
Old 04-03-2012
Create a dummy file in all directories that include a .jpg

Hello. My latest project has me with the need for the following script. Basically, any directory that includes a .jpg file needs to also have a ".picture" file created (if it doesn't exist). Here's an example of what I need.

Code:
/mnt/user/Pictures/2011/Hawaii - 2011/.picture
/mnt/user/Pictures/2011/Hawaii - 2011/Hawaii.jpg
/mnt/user/Pictures/2012/Mammoth - family - 2012/Mammoth 001 - 2012.jpg

In this example, I want to recursively scan all directories in /mnt/user/Pictures/, and create the .picture file in any directory that has a .jpg file within it (/mnt/user/Pictures/2011/Hawaii - 2011/, for example, does not include a .jpg file). The only directory that would need the .picture file in this example would be /mnt/user/Pictures/2012/Mammoth - family - 2012/. This .picture file is an empty dummy file, and can created as simply as "echo > .picture".

I don't mind recreating the .picture file in each directory, regardless of if it already exists or not. I thought it might be cleaner to only create it if it's needed, but it's not that big of a deal to recreate it each time regardless.


Any thoughts on the code for this script? I started going down the path of using:
Code:
find /mnt/user/Pictures/ -name \*.jpg -print

... however, I quickly hit a brick wall after that.

Thanks for the help in advance!!
# 2  
Old 04-03-2012
I'd try looking for folders instead, so you don't have to extract what picture's in what folder. Seems easier to do vice-versa.

Code:
find /mnt/user/Pictures/ -type d | while read FOLDER
do
        if ls "$FOLDER" | grep "\.jpg$" > /dev/null
        then
                touch "$FOLDER"/.picture
        fi
done

# 3  
Old 04-03-2012
Try:
Code:
find /mnt/user/Pictures/ -type f -name "*.jpg" -exec dirname {} \; | sort | uniq | xargs -i touch {}/.picture

# 4  
Old 04-03-2012
Thanks for the prompt replies!


Quote:
Originally Posted by bartus11
Try:
Code:
find /mnt/user/Pictures/ -type f -name "*.jpg" -exec dirname {} \; | sort | uniq | xargs -i touch {}/.picture

When I try this solution, I get the following error:

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option


Quote:
Originally Posted by Corona688
I'd try looking for folders instead, so you don't have to extract what picture's in what folder. Seems easier to do vice-versa.

Code:
find /mnt/user/Pictures/ -type d | while read FOLDER
do
        if ls "$FOLDER" | grep "\.jpg$" > /dev/null
        then
                touch "$FOLDER"/.picture
        fi
done

When I try this solution, it doesn't seem to copy the .picture files to the folders. It seems to finish fine, but none of the folders have the .picture file. I tried changing the touch "$FOLDER"/.picture command to echo $FOLDER, and nothing was echo'd.
# 5  
Old 04-03-2012
What system are you using? Anyway, try this:
Code:
find /mnt/user/Pictures/ -type f -name "*.jpg" -exec dirname {} \; | sort | uniq | xargs -i touch "{}/.picture"

# 6  
Old 04-03-2012
This is an unRAID system. It's essentially a trimmed down Slackware installation.
# 7  
Old 04-03-2012
Try removing the if-statement, then, see if it can echo $FOLDER then.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create movie from jpg (or other picture format) file

hi linux expert is there any program for create movie file from pictures file (like jpg)? Many Thanks samad (1 Reply)
Discussion started by: abdossamad2003
1 Replies

2. Shell Programming and Scripting

Bash to create sub directories from specific file extension

In the bash below I am trying to create sub-directories inside a directory from files with specific .bam extensions. There may be more then one $RDIR ing the directory and the .bam file(s) are trimmed (removing the extension and IonCode_0000_) and the result is the folder name that is saved in... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Create pdf of a jpg image in shell script

Dear Team, Can any one please let me know, if there is any way to create pdf of a Jpg image file in shell script. I work on Solaris, Korn Shell. Currently we are using sunpcl2pdf.exe to create PDF from PCL(Printer Control Language) files. But we are searching for some different way to... (2 Replies)
Discussion started by: Uttam Maji
2 Replies

4. Shell Programming and Scripting

Help with create multiple directories under diff file systems

Hi, Need help ...I want to create multiple directories in different /file systems using for loop..eg.../ORCL_data01/oradata/orcl/ctl. ../ORCL_data01/oradata/orcl/data. ../ORCL_data01/oradata/orcl/redo. Script :- ========= for dir in `ls -d... (8 Replies)
Discussion started by: Linux6.5
8 Replies

5. UNIX for Dummies Questions & Answers

Converting pdf to jpg in multiple directories?

Hi! On my Ubuntu I have thousands of files in a couple hundred directories. I'm trying to convert the pdf's to jpg's. I used this command in terminal: for fname in *.pdf; do convert $fname ${fname%.pdf}.jpg; doneIt works great but the problem is I have to run this in terminal for each... (2 Replies)
Discussion started by: martinsmith
2 Replies

6. Linux

Create a dummy file even if the path is not exist

hi, i want to create a dummy file even if the path to that file is not exist as follows. suppose, in my working directory 2 files are there. and i create one more file which is exist as follows # pwd /home/satya # ls file1.txt file2.txt # echo dummy > /home/satya/file3.txt # ls... (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

7. Shell Programming and Scripting

How to create tgz file for all the directories in one time?

Hi, On server there is an one folder . which contains sub folder Eg - TEST folder contains test1, test2, execr ,tt (folder). also includes some text file like abc.txt psp.txt gg.log. here iwant to create tgz file for all the folders and file in one time. I know the command... (1 Reply)
Discussion started by: aish11
1 Replies

8. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

9. UNIX for Dummies Questions & Answers

Create directories from a sql file?

Hi, I have a sql file which has CREATE and INSERT commands in it. Basically, inside this file, a table will be created and the data will be inserted into the table. I was wondering if there is a way for me to create directories from this file? Thanks in advance (4 Replies)
Discussion started by: tezarin
4 Replies

10. SCO

Create dummy printer

On SCO Openserver we create a so called dummy printer that directs to /dev/null (we use this in our software to purge some stuff). Now I have SCO UnixWare 7.1.4, but I cannot create a dummy printer. I create the printer exactly as in Openserver by the Printer Manager, - Enter spoolname -... (1 Reply)
Discussion started by: p.vvugt
1 Replies
Login or Register to Ask a Question