Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: How to create filename with sub directories

  1. #1
    Join Date
    Aug 2011
    Location
    India
    Posts
    250

    Default How to create filename with sub directories

    Hi I am Rupesh from India. I have a text file which contains filenames preceded by directories.

    The text file contains the following pattern in all the lines
    ./a/b/c/d/e/temp.mp3.

    I want to create a file with name temp.mp3 which is contained in directory e which is contained in directory d which is contained in directory c which is contained in directory b which is contained in directory a which is contained in the current directory.

    I think that we can create file using touch.

    I have issued the following command

    touch ./a/b/c/d/e/temp.mp3

    The above command displayed message as ./a/b/c/d/e/temp.mp3 not found.

    Please suggest how to create filename with sub directories.

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,251

    Default Re: How to create filename with sub directories

    First, you should always post what you do complete here between CODE tags. Thus not that long story of you but:
    Code:
    henk@boven:~/test/rupesh> LANG=C touch ./a/b/c/d/e/temp.mp3 
    touch: cannot touch './a/b/c/d/e/temp.mp3': No such file or directory
    henk@boven:~/test/rupesh>
    only so can we see what you saw.

    Second, read
    Code:
    man touch
    it says that it creates FILE, not a PATH. So there is really no need to try a complete path because that does not work by definition.,

    Third, you create a directory using mkdir. And a complete PATH of directories with mkdir -p, Again read
    Code:
    man mkdir
    example:
    Code:
    henk@boven:~/test/rupesh> mkdir -p ./a/b/c/d/e/
    henk@boven:~/test/rupesh> touch ./a/b/c/d/e/temp.mp3 
    henk@boven:~/test/rupesh> ls -l ./a/b/c/d/e/temp.mp3 
    -rw-r--r-- 1 henk wij 0  3 okt 09:24 ./a/b/c/d/e/temp.mp3
    henk@boven:~/test/rupesh>
    Henk van Velden

  3. #3
    Join Date
    Aug 2011
    Location
    India
    Posts
    250

    Default Re: How to create filename with sub directories

    Actually the text file consists of 12000 filenames. All of you are talking about creating a single file.

    May I know how to delete the last column of a file. I have read manual page and info page of cut it has specified about field option ie -f but it doesn't describe about cutting from backwards. I have used cut to delete first column from a file and it has displayed some output showing first column of a file but when I opened the original file it is displaying the first column as before I mean first column has not been deleted.

    Please try to suggest how to delete last column from a file if not atleast how to delete first column from a file. By using rev utility I can reverse all the characters present in a file and after that I can remove first column using cut then again if I use rev utility I can achieve what I want.
    Regards,
    Rupesh.

  4. #4
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,251

    Default Re: How to create filename with sub directories

    Code:
    man basename
    Code:
    man dirname
    BTW, what are you going to pay me for all this advice?
    Henk van Velden

  5. #5
    graham NNTP User

    Default Re: How to create filename with sub directories

    rupeshforu3 wrote:

    >
    >
    > Hi I am Rupesh from India. I have a text file which contains filenames
    > preceded by directories.
    >
    > The text file contains the following pattern in all the lines
    > ./a/b/c/d/e/temp.mp3.
    >
    > I want to create a file with name temp.mp3 which is contained in
    > directory e which is contained in directory d which is contained in
    > directory c which is contained in directory b which is contained in
    > directory a which is contained in the current directory.
    >
    > I think that we can create file using touch.
    >
    > I have issued the following command
    >
    > touch ./a/b/c/d/e/temp.mp3
    >
    > The above command displayed message as ./a/b/c/d/e/temp.mp3 not found.
    >
    > Please suggest how to create filename with sub directories.
    >
    >

    Try touch a/b/c/d/e/temp.mp3
    note the missing ./

  6. #6
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    27,099
    Blog Entries
    15

    Default Re: How to create filename with sub directories

    Quote Originally Posted by rupeshforu3 View Post
    Hi I am Rupesh from India. I have a text file which contains filenames preceded by directories.

    The text file contains the following pattern in all the lines
    ./a/b/c/d/e/temp.mp3.

    I want to create a file with name temp.mp3 which is contained in directory e which is contained in directory d which is contained in directory c which is contained in directory b which is contained in directory a which is contained in the current directory.

    I think that we can create file using touch.

    I have issued the following command

    touch ./a/b/c/d/e/temp.mp3

    The above command displayed message as ./a/b/c/d/e/temp.mp3 not found.

    Please suggest how to create filename with sub directories.
    Hi
    Enjoy....

    Save as read_file;

    Code:
    #!/bin/bash
    
    # Counter if multiple lines to process
    count=1
    while IFS='' read -r line || [[ -n "$line" ]]; do
        # Check line output to process
        echo "Line $count text is: $line"
        # Get path and remove leading . and /
        path=`echo $(dirname "${line}")| sed 's/.\///'` 
        echo "Path is $path"
        # Make the directory tree if it exists or not
        mkdir -p $path
        # Get the filename
        filename="${line##*/}"
        echo "Filename is $filename"
        # Create the filename
        touch $path/$filename
        count=`expr $count + 1`
    done < "$1"
    A sample file;

    Code:
    cat somefile.txt
    
    ./a/b/c/d/e/temp1.mp3
    ./a/b/c/d/e/temp2.mp3
    ./a/b/c/d/e/temp3.mp3
    ./f/g/h/i/j/temp4.mp3
    Make saved script executable and run with filename input;

    Code:
    chmod 0755 read_file
    ./read_file somefile.txt
    
    Line 1 text is: ./a/b/c/d/e/temp1.mp3
    Path is a/b/c/d/e
    Filename is temp1.mp3
    Line 2 text is: ./a/b/c/d/e/temp2.mp3
    Path is a/b/c/d/e
    Filename is temp2.mp3
    Line 3 text is: ./a/b/c/d/e/temp3.mp3
    Path is a/b/c/d/e
    Filename is temp3.mp3
    Line 4 text is: ./f/g/h/i/j/temp4.mp3
    Path is f/g/h/i/j
    Filename is temp4.mp3
    Result;

    Code:
    ls
    a  f  read_file  somefile.txt
    
    ls a/b/c/d/e/
    temp1.mp3  temp2.mp3  temp3.mp3
    
    ls f/g/h/i/j/
    f/g/h/i/j/temp4.mp3
    Last edited by malcolmlewis; 03-Oct-2017 at 09:15.
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  7. #7
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    27,099
    Blog Entries
    15

    Default Re: How to create filename with sub directories

    Quote Originally Posted by pwilson View Post
    Same as he's giving everyone else in all the other forums where he looks for handouts
    Hi
    You seem to have some issues with this, the Forum staff don't. Please see the forum T&C's regarding these are technical forums for users to ask question. Help, don't help but trolling/stalking won't be.
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  8. #8
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,251

    Default Re: How to create filename with sub directories

    Quote Originally Posted by pwilson View Post
    Same as he's giving everyone else in all the other forums where he looks for handouts:
    https://www.linuxquestions.org/quest...es-4175614922/
    What is your intention on repeating and repeating your song? You are at least as stubborn as rupesh is.

    You need a vacation from the forums. And will be banned for three weeks.

    (And you can still read our T&C as malcomlewis advices when not being logged in).
    Henk van Velden

  9. #9
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    11,378
    Blog Entries
    2

    Default Re: How to create filename with sub directories

    First,
    You're only going to get "best effort" advice if you don't post actual, real examples.
    Do not post some general description or the only thing you'll get is some general answer which may or may not work.

    Using touch to create a file in a directory.
    You can do so for any location where all directories in the path already exist.
    The command will not magically create directories when you create a file.
    And, it should not matter whether you include "this directory" ie ./foo

    Removing "column"
    Can't possibly understand what you mean.
    Again, you have to post an actual file or file name and what you want to do with it.

    Don't make people guess at what you're asking.
    Be detailed and exact about what you're asking.

    HTH,
    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  10. #10

    Default Re: How to create filename with sub directories

    Hi,

    I just want to add that touch is not just for creating empty files. Creating empty files is just a bonus of touch
    Code:
    DESCRIPTION
           Update the access and modification times of each FILE to the current time.
    
           A FILE argument that does not exist is created empty, unless -c or -h is supplied.
    
           A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output.
    
           Mandatory arguments to long options are mandatory for short options too.
    The shell can create an empty file by itself, well at least POSIX shells that supports redirection ( I hope )
    Redirection means using the More than > and Less than sign < (or operators as what the shell docs calls it) . In this case the more than sign is to be used.

    Code:
    > foo.mp3
    Should create an empty file named foo.mp3.

    Here is an example. First go to an empty directory and run the commands below.

    Code:
    mkdir -p foo/bar/baz/more/ && > "${_}foo.mp3"
    Code:
    tree
    Code:
    .
    └── foo
        └── bar
            └── baz
                └── more
                    └── my.mp3
    
    4 directories, 1 file
    I'm not sure if tree is installed by default though. Also Pathname is another word for directories since you want to separate the Filename and Pathname.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

Page 1 of 2 12 LastLast

Posting Permissions

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