Hi, I have around 500 directories, and I wish to first create an empty text file within each of these and then process my data files contained within sub-directories and redirect the output into the respective text files.
An example of my existing folders - exisiting/main/sub1/sub2/sub3/sub4/sub5/sub6/data.tsv.bz2
What I wish to do - create main/sub1.txt & extract data.tsv.bz2 > main/sub1.txt
I have figured how to redirect my output into the desired files, but I am facing issues with creating the .txt files.
Tha command tried for the same is for i in */; do touch $i.txt; done. This creates a file named .txt within the main folder. So I also tried for i in */ ; do cd "$i"; for j in */; do ( touch $j.vcf ) done; done, which gave me an error saying
bash: cd: main/: No such file or directory
Since I have typed the command from my existing directory, it first identifies the main directory. I want to be able to provide the name of the sub1 directory while creating sub1.txt
Is there any way to get around this problem?
Thanks in advance
0 answers
No answers yet.
Log in to answer this question.
You didn't mention where the sub directories are. Here is the solution to OP problem:
As mentioned by @cpad0112 The problem statement is not very clear.
Probable solution
Thank you both for your response. I am sorry that my problem was not very clear. I am working from the directory named
existing/, which contains subdirectories such asmain/sub1/sub2/sub3/sub4/sub5/sub6/.sub6/contains the filedata.tsv.bz2. I wish to create a file calledsub1.txtin themain/directory. Technically, I would have saidtouch main/sub1.txt, but since I have 500 directories, say for examplemain1/,main2/,main13/,main27/etc, I wish to do this using afor loopsuch asfor i in */ ; do cd "$i"; for j in */; do ( touch $j.vcf ) done; done. Here, I tried to identifymain/with$iandsub1/with$jis
sub1/sub2/sub3/sub4/sub5/sub6/a fixed string? As I understand, main is not (main 1..main 500). It would help if you could post directory tree.Starting from the directory
existing/├── main/
│ └── sub1/
│ └── sub2/
│ └── sub3/
│ └── sub4/
│ └── sub5/
| └── sub6/
| └── data.tsv.bz2
Since OP information is incomplete, the best you can do is:
output should look like this. Bash command above dry-runs the code:
Dry run with parallel:
This works fine. Thanks a lot. You may move it to an answer so that I can accept it. Thank you once again :)
So, will this extract
data.tsv.bz2intosub1.txt?I would also like to know if I can create files with
touchin the way I mentioned in the post?Thanks once again.
Take a back up of one file and run one command from the dry-run on the backed-up directory/file. you would know if it works or not.