LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-03-2017, 01:26 AM   #1
rupeshforu3
Member
 
Registered: Jun 2013
Location: India
Distribution: any Linux, BSD, Solaris, sco unixware, Windows 8
Posts: 59

Rep: Reputation: 0
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.

Regards,
Rupesh
 
Old 10-03-2017, 01:36 AM   #2
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 3,419
Blog Entries: 9

Rep: Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899
touch can create files but not directories. You can create your directory chain with mkdir -p, which creates a directory and its parent(s). Then use touch to add the file.

If you are taking the names from a file, you may need to write a script to process it. You can use cut -f to extract the filename and the last directory name from each line. Read the cut man page for more details.

Last edited by hazel; 10-03-2017 at 01:56 AM.
 
Old 10-03-2017, 02:38 AM   #3
IsaacKuo
Senior Member
 
Registered: Apr 2004
Location: Baton Rouge, Louisiana, USA
Distribution: Debian 9 Stretch
Posts: 2,349
Blog Entries: 8

Rep: Reputation: 384Reputation: 384Reputation: 384Reputation: 384
I just thought of a silly hack which would avoid "cut". Consider what this sequence of commands would do:
Code:
mkdir -p /a/b/c/d/e/temp.mp3
rmdir    /a/b/c/d/e/temp.mp3
touch    /a/b/c/d/e/temp.mp3
Silly, right? But it would work...
 
Old 10-03-2017, 03:50 AM   #4
rupeshforu3
Member
 
Registered: Jun 2013
Location: India
Distribution: any Linux, BSD, Solaris, sco unixware, Windows 8
Posts: 59

Original Poster
Rep: Reputation: 0
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.
 
Old 10-03-2017, 04:47 AM   #5
rupeshforu3
Member
 
Registered: Jun 2013
Location: India
Distribution: any Linux, BSD, Solaris, sco unixware, Windows 8
Posts: 59

Original Poster
Rep: Reputation: 0
Actually the text file consists of 12000 filenames. All of you are talking about creating a single file.
 
Old 10-03-2017, 05:33 AM   #6
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 3,419
Blog Entries: 9

Rep: Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899Reputation: 1899
Something like cut gives you an output but doesn't change your input file. You need to direct your output into a new file.

You don't say if the lines all have the same number of directories specified. If so, you can easily work out the correct field numbers for last directory and the filename. If not, then your idea of using rev twice would work.

Another possibility is to write an awk script to analyse your file. The advantage of that is that awk naturally works line by line, passing the results to standard output.
 
1 members found this post helpful.
Old 10-03-2017, 11:03 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,974

Rep: Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831
Quote:
Originally Posted by rupeshforu3 View Post
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.
...and....
Quote:
Originally Posted by rupeshforu3
Actually the text file consists of 12000 filenames. All of you are talking about creating a single file.
Wow...this sounds VERY familiar. 12000 names, huh? With a directory structure? Just like this:
https://www.linuxquestions.org/quest...ts-4175610839/

...followed up by:
https://www.linuxquestions.org/quest...te-4175614825/

And now we're back here again? Hazel gave some good advice, but what do you mean by "delete the last column of a file"??? Because that reads as "I want to open a file with 10 columns, and delete the last column IN that file". Do you mean you want to strip off the extension, like (COINCIDENTALLY, I'm positive), your other threads about MP3's you want to download in bulk from that website, using wget? Is this because you haven't read/understood the wget man page about recursively doing this yet, after two solid months?

Awk is one way; but as you've been told MANY times previously, why can't you write a simple bash script to read that file that you say you have, loop through it line by line, and run a command? Again, examples are plentiful, and even the most BEGINNER level bash scripting tutorials cover reading a file and looping. Since you've been asking about scripting for years now, can you show us ANY of the efforts you've put forth to accomplish what you're after??? Or is this just the first in what you mentioned here, where you say you're just going to keep changing the wording, until someone does this for you?
http://www.linuxforums.org/forum/net...tml#post991576
 
Old 10-03-2017, 11:14 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,533
Blog Entries: 9

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
TB0ne, you are absolutely & 1000% right!
there's degrees of avoiding effort, but op is definitely on top of that list!

and i am not encouraging him/her!!!

but...
Quote:
Originally Posted by IsaacKuo View Post
avoid "cut"
i'd rather use bash itself.
e.g.
Code:
filename="/file/with/full/path.mp3"
mkdir -p ${filename##*/}
touch "$filename"
http://www.tldp.org/LDP/abs/html/str...ipulation.html

Last edited by ondoho; 10-03-2017 at 11:16 AM.
 
Old 10-03-2017, 11:31 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,974

Rep: Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831
Quote:
Originally Posted by ondoho View Post
TB0ne, you are absolutely & 1000% right!
there's degrees of avoiding effort, but op is definitely on top of that list! and i am not encouraging him/her!!! but...

i'd rather use bash itself. e.g.
Code:
filename="/file/with/full/path.mp3"
mkdir -p ${filename##*/}
touch "$filename"
http://www.tldp.org/LDP/abs/html/str...ipulation.html
Exactly, and the OP has been pointed to/asked to look at bash scripting tutorials for years, and they never seem to. If rupesh spent 1/10th the time he spends posting, doing research/trying/learning, he'd not have to continually ask for handouts.
 
1 members found this post helpful.
Old 10-03-2017, 11:50 AM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,533
Blog Entries: 9

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
wrong string manip.
should've been
Code:
mkdir -p "${filename%/*}"
 
1 members found this post helpful.
Old 10-03-2017, 01:07 PM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,974

Rep: Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831Reputation: 5831
Quote:
Originally Posted by ondoho View Post
wrong string manip.
should've been
Code:
mkdir -p "${filename%/*}"
Shame that you gave him the solution he's been after for two months, and did nothing on his own to achieve it.
 
1 members found this post helpful.
Old 10-03-2017, 08:02 PM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,204

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
Code:
mkdir -p "${filename%/*}"
is a little faster than running the dirname program.
Code:
mkdir -p "$(dirname "$filename")"
dirname returns . if there is no / in filename. Obviously not the case here.
Now put that in a while loop
Code:
while read filename
do
  mkdir ... 
  > "$filename"
done < file_of_filenames
The builtin > creates an empty file, and is little faster than running the touch program. Caution: unlike touch, the > empties an existing file!

Last edited by MadeInGermany; 10-03-2017 at 08:07 PM.
 
Old 10-04-2017, 12:29 AM   #13
!!!
Member
 
Registered: Jan 2017
Posts: 666

Rep: Reputation: 289Reputation: 289Reputation: 289
Quote:
Originally Posted by TB0ne View Post
Shame that you gave him the solution he's been after for two months, and did nothing on his own to achieve it.
From this web-search, I've come to realize some people have extreme difficulty with 'soft skills'. What I might be able to contribute here is: my suggestion that you may find Threads in the ZRT (esp 2+days old) that are far more deserving of expert help (time&effort).

Please don't be offended by my posting this suggestion; I didn't intend to divert this to a discussion.

Last edited by !!!; 10-04-2017 at 02:14 AM. Reason: Changed word "more" to "extreme", as pointed out about OP.
 
Old 10-04-2017, 01:44 AM   #14
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,533
Blog Entries: 9

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
Quote:
Originally Posted by TB0ne View Post
Shame that you gave him the solution
i didn't.
it's one building block to the solution (granted, a very simple solution that only consists of 2 or 3 building blocks).
put all the help received so far together, everything needed is now in this thread.
i suspect that like before, op won't be able to put the pieces together or say thanks.
or even communicate properly with us.
because all rupeshforu threads look like this.
 
Old 10-04-2017, 02:55 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,103

Rep: Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145
Quote:
Originally Posted by MadeInGermany View Post
Code:
mkdir -p "${filename%/*}"
is a little faster than running the dirname program.
Code:
mkdir -p "$(dirname "$filename")"
dirname returns . if there is no / in filename. Obviously not the case here.
Now put that in a while loop
Code:
while read filename
do
  mkdir ... 
  > "$filename"
done < file_of_filenames
The builtin > creates an empty file, and is little faster than running the touch program. Caution: unlike touch, the > empties an existing file!
little faster?
touch and dirname are new processes, that means a fork and an exec and finally the execution of (almost) the same routine (creating a file/calculating dir name).
This is approximately million(!) times longer, requires huge amount of additional memory and other resources.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script help: create a filename from last column SmurfGGM Linux - General 2 09-02-2014 11:22 AM
How do i make a diff between directories that ignores part of the filename pandersson61 Programming 4 12-16-2012 07:15 AM
find string in filename and use string to create directories daberkow Linux - Newbie 11 05-01-2009 02:12 PM
How to create a tar without input filename v_t_long Linux - General 1 11-09-2008 08:13 AM
Append md5sum or similar to filename, batch copy from multiple directories to one migla Linux - Newbie 5 02-03-2008 05:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:11 PM.

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