Dynamically generate directory structure from text file - bash

Given the following text file which contains a list of directories, how would you automatically generate the file path if it doesn't exist.
apache/bin/
apache/build/
apache/error/include
apache/htdocs/images/
apache/icon/small
python/bin
python/include/python2.7
lib/pkconfig
lib/python2.7/bsddb/test
lib/python2.7/compiler
lib/python2.7/ctypes/macholib
lib/python2.7/ctypes/test
For example, from the root/start directory, if the line reads apache/bin/, I want to check if the folder apache exists, if it doesn't, then I create it.
After that I want to cd into it, check if the file bin exists, if it doesn't then I create it. Then before moving into the new line, I want to move back into the root directory (i.e. move up two levels).
How would I accomplish this in Bash?

Related

Replace files in multiple folders, whose name includes destination folder name

I have very little experience with the command line and I'm trying to do something very complicated (to me).
I have a directory with A LOT of subfolders and files in them. All file names contain the parent folder name, e.g.:
Folder1
data_Folder1.csv
other_file_Folder1.csv
Folder2
data_Folder2.csv
other_file_Folder2.csv
In another folder (all in one directory), I have a new version of all the data_FolderX.csv files and I need to replace them in the original folders. I cannot give them another name because of later analyses. Is there a way to replace the files in the original folders with the new version, in the command line?
I tried this Replacing a file into multiple folders/subdirectories but didn't work for me. Given that I have many .csv files in the derectories, I don't want to replace them all, so I don't think I should do it based on the file extension. I would also like to note that the name "FolderX" contains several other _, so in principal, I want to replace the .csv file starting with data in the FolderX.
Can anyone help?
Thanks in advance!

Bash script for editing multiple text files

I would like some guide and help for this:
I have a text file with names listed in it. I will call it “source file”
I have multiple text files scattered in folders and sub folders
I would like to know how to make a script that would automatically add SPECIFIC LINE to every text file (in subfolders of chosen target folder) that contains exact name listed in “source file”
More detailed/Example:
I have a names.txt that contains many names. I want to find all the text files in target folder and it’s subfolders that contains names listed in names.txt and in those files automatically add “FALSE” line (in front or after specific existing line).

Linux Script to check for file name

In our inbound directory we have the following file structure.
abc.csv.timestamp
abc.csv.timestamp
abc.csv.timestamp
I want to find the most recent file in the directory and MOVE to another directory.
However i want to pass the file pattern abc.csv.* as a string to the command.
Any pointers ?

vim set tags with absolute path

I work on multiple projects, each ~3-5 million lines of code. I have a single tags file at the root of each project. I also have a tools directory shared between all of them.
disk1
|
+--Proj A
|
+--Proj B
|
+--Shared
disk2
|
+--Proj C
|
+--Proj D
When using tags, I would like Vim to first search the tags file at the root of my project, and then search the tags file for Proj X, and then search the tags file in Shared
I can't get Vim to find the tags file in Shared
in my .vimrc file I have:
set tags=tags;D:/Shared
set tags=tags;,D:/Shared (thanks to romainl for catching a missing comma!)
but Vim only searches the local project tags file, not the shared one.
tags; should start at the CWD and traverse back up the tree until a tags file is found (finds the correct one at the project level).
D:/Shared is an explicit path and should find the tags file in that directory but fails to do so (I've checked, it does in fact exist).
I'm using Exuberand Ctags v5.8
set tags=tags;D:/Shared
means "look upward for a tags file from the current directory until you reach D:/Shared".
If you work in project C on disk 2 (let's call that disk E:), Vim will never visit D:/Shared because of two things:
Upward search is not recursive.
If no tags file is found at the root of the "current directory", Vim tries to find one at the root of its parent and so on until it reaches the topmost parent or the directory you specified after the semicolon. So, supposing you are editing E:\ProjectC\path\to\some\file, you can't expect Vim to find a tags file outside of that path. Vim will search for the following tags files, sequentially and, by the way, never find that hypothetic D:\Shared:
E:\ProjectC\path\to\some\tags <-- KO
E:\ProjectC\path\to\tags <-- KO
E:\ProjectC\path\tags <-- KO
E:\ProjectC\tags <-- OK!
E:\tags <-- KO
It won't find any tags file not listed above.
Windows doesn't have the equivalent of UNIX's "root" directory anyway.
When you don't specify a stop directory, upward search climbs the inverted tree of your filesystem from the current directory (or an arbitrary start directory) to the root of the filesystem.
Supposing you are still editing E:\ProjectC\path\to\some\file, upward search will ultimately look for the stop directory D:\Shared directly under every parent directory in the path to E:\ and will rather obviously never find it.
If you want Vim to find D:\Shared\tags wherever you are, you only need to add it explicitely to the tags option. Not as a stop directory but as a specific location:
set tags=tags;,D:/Shared/tags
Now, it says "look upward for a tags file from the current directory and use D:/Shared/tags".
Hmm… that was a lot of words just to explain the need for a single ,.

Terminals - Creating Multiple Identical Folders within Subdirectories and Moving Files

I have a bunch of files I'm trying to organize quickly, and I had two questions about how to do that. I really appreciate any help! I tried searching but couldn't find anything on these specific commands for OSX.
First, I have about 100 folders in a directory - I'd like to place an folder in each one of those folders.
For example, I have
Cars/Mercedes/<br>
Cars/BMW/<br>
Cars/Audi/<br>
Cars/Jeep/<br>
Cars/Tesla/
Is there a way I can create a folder inside each of those named "Pricing" in one command, i.e. ->
Cars/Mercedes/Pricing <br>
Cars/BMW/Pricing<br>
Cars/Audi/Pricing<br>
Cars/Jeep/Pricing<br>
Cars/Tesla/Pricing
My second question is a little tougher to explain. In each of these folders, I'd like move certain files into these newly created folders (above) in the subdirectory.
Each file has a slightly different filename but contains the same string of letters - for example, in each of the above folders, I might have
Cars/Mercedes/payment123.html
Cars/BMW/payment432.html
Cars/Audi/payment999.html
Cars/Jeep/payment283.html
Is there a way to search each subdirectory for a file containing the string "payment" and move that file into a subfolder in that subdirecotry - i.e. into the hypothetical "Pricing" folders we just created above with one command for all the subdirectories in Cars?
Thanks so much~! help with either of these would be invaluable.
I will assume you are using bash, since it is the default shell in OS X. One way to do this uses a for loop over each directory to create the subdirectory and move the file. Wildcards are used to find all of the directories and the file.
for DIR in Cars/*/ ; do
mkdir "${DIR}Pricing"
mv "${DIR}payment*.html" "${DIR}Pricing/"
done
The first line finds every directory in Cars, and then runs the loop once for each, replacing ${DIR} with the current directory. The second line creates the subdirectory using the substitution. Note the double quotes, which are necessary only if the path could contain spaces. The third line moves any file in the directory whose name starts with "payment" and ends with ".html" to the subdirectory. If you have multiple files which match this, they will all be moved. The fourth line simply marks the end of the loop.
If you are typing this directly into the command line, you can combine it into a single line:
for DIR in Cars/*/ ; do mkdir "${DIR}Pricing"; mv "${DIR}payment*.html" "${DIR}Pricing/"; done

Resources