Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: any Linux, BSD, Solaris, sco unixware, Windows 8
Posts: 59
Rep:
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.
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.
Distribution: any Linux, BSD, Solaris, sco unixware, Windows 8
Posts: 59
Original Poster
Rep:
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.
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.
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.
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
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...
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.