Find the answer to your Linux question:
Results 1 to 5 of 5
I have a directory full of jpgs and zips. I want to create an empty copy of that file. If I have test.jpg. I want to create test.jpg in another ...
  1. #1
    Just Joined!
    Join Date
    Feb 2007
    Posts
    4

    Create New Empty File

    I have a directory full of jpgs and zips. I want to create an empty copy of that file. If I have test.jpg. I want to create test.jpg in another directory that's pretty much an empty file.

    I have this:
    for file in `find . -type f`; do SOMETHING; done;

    Is there an easy way to do this?

    Any help would be great!

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    If you want to recreate the same structure, you can use find -type d to find first all the directories with -exec mkdir to recreate them. You might need to parse a bit the paths depending on the way the script is going to operate.

    After that, you can use find -type f to find the files. The usual command to create an empty file is touch.

  3. #3
    Linux Newbie
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    112
    Code:
    #!/bin/bash
    SRC=/source/dir
    DEST=/dest/dir
    
    cd $SRC
    for file in `ls -al *.conf | awk '{print $8}'` 
    do 
        touch $DEST/$file
        echo "Empty File created $DEST/$file "
    done
    - nilesh
    bigunix.blogspot.com
    Registered Linux User: #476440

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Hi,

    Quote Originally Posted by ynilesh View Post
    Code:
    #!/bin/bash
    SRC=/source/dir
    DEST=/dest/dir
    
    cd $SRC
    for file in `ls -al *.conf | awk '{print $8}'` 
    do 
        touch $DEST/$file
        echo "Empty File created $DEST/$file "
    done
    - nilesh
    I discourage the usage of ls to get file names, since it will screw up if the file names contain any space or the ls output format changes. Parsing the ls output is complex and non-reliable. Plus this is just simpler:

    Code:
    for file in *.conf
    do
    ...
    done
    Aditional advantages are that you don't have to launch ls, nor awk, nor to open a pipe, which are all a waste of resources.

    If you insist on using ls, at least make sure the format is correct:

    Code:
    ls -1 *.conf | while read file
    do
    ...
    done
    Note that "-1" is a number one, not an 'l' letter. At least that way you ensure you are reading more consistent data (though I still see no point in using ls).It just make the things more complicated and error prone.

    Anyway, this will not replicate the directory tree as the original posted wanted. The 'find' approach is the simplest because it recurses through the directory tree.

  5. #5
    Linux Newbie
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    112
    Thanks, i was not knowing it
    bigunix.blogspot.com
    Registered Linux User: #476440

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...