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.

First, you should always post what you do complete here between CODE tags. Thus not that long story of you but:

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

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

man mkdir

example:

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>

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.

man basename
man dirname

BTW, what are you going to pay me for all this advice?

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 ./

Hi
Enjoy…

Save as read_file;


#!/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;


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;


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;


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

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.

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).

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

Hi,

I just want to add that touch is not just for creating empty files. Creating empty files is just a bonus of touch :slight_smile:


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 :slight_smile: )
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.

> 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.

mkdir -p foo/bar/baz/more/ && > "${_}foo.mp3"
tree
.
└── 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.

The text file consists of URL characters like %20, %26, %29 etc. This text file was created by an internet tool called wget.

I tried to create a folder from one line of the text file and the resulted folder path contained these URL characters.

Can you suggest how to create folders and files which contains the converted URL characters.

Hi
You need to show an example of the actual raw data, descriptions can be deceptive, so show the line of what you get via wget, then what the expected output your wanting.

You could paste the file “as is” into an online URL Encode/Decode utility, for example: https://www.url-encode-decode.com/ and then use the resulting plain text output. Subject to any size limitations on the conversion site of course, you may have to do it in chunks.

The text file consists of 12000 lines. May I know is there any tool in order to convert all the URL characters automatically.

https://opinionatedgeek.com/Codecs/UrlDecoder

Follow the (simple) instructions on the page…

Does it work?

Yes - Good, Job Done :slight_smile:

No - Oh Dear :frowning: Ask again(!) or maybe roll your own solution shell script - Decoding URL encoding (percent encoding) - Unix & Linux Stack Exchange

Best of luck, I’m getting my coat.

Hi,

So the raw url that has ** %20, %26, %29** characters is not clear but wget has the ability to convert urls.

grep -i convert < <(man wget)

or using a pager

PAGER='less +/convert' man wget

Just press the **n **key or button to jump to the next key word, in this case **convert
**