Inserting directory in existing directory structure in unix - unix

I have a directory structure something like :
/etc/home/d1/d2/d3/d4
The last directory d4 contains some files so it is not empty.
But by mistake I forgot to create one more directory in between say d0
So I need to change my directory structure to :
/etc/home/d0/d1/d2/d3/d4
So my question is - is there any way to introduce this new directory in existing path or I have to do all the donkey work? :P

Create a directory under your home directory with name d0. Then use the mv command to move d1 to d0. All directories and files under d1 should get moved to d0 giving you the desired structure.

mkdir /etc/home/d0
mv /etc/home/d1 /etc/home/d0
This is quite different from the single command (when /etc/home/d0 does not exist as a directory):
mv /etc/home/d1 /etc/home/d0
That might be a little confusing. The first creates a directory and moves the hierarchy into it. The second just renames one level in the hierarchy, which was not what you wanted.

Related

I need to copy all files but one from one dircetory to another

Total noob question here, but I want to copy every file but file1.txt from 1 directory (i am not in this directory) to another directory using wildcards specificatlly using *
Any help?

Moving files from current directory to one directory below

I am trying to move 2 files from current directory (/base/level1/level2) to one directory below (/base/level1)
Is there any easier command other than mv file1 /base/level1 ? I'm trying to understand if we have some command that move it to a specific level up or down in the current folder structure.
TIA!
My solution to this one is navigating to the path where I want to move the file and run the following command.
cp ./level2/file1 .
or
cp ./file1 ./level2/file1
Please share other solutions as well
Thanks,
AMK
You could use wildcards if the 2 files have something uniquely in common.
ie. mv file*.ext path/to/new/dest/
This will move all files starting with "file" and ending with the extension ".ext" to the destination. Have a look at this and this which will explain wildcards a bit more
You can always use .. for "one directory up".
And you can give more than 2 arguments to mv, the last always being the destination.
So mv file1 file2 .. would move those 2 files a directory up.
Or mv * .. to move all files.

How to list contents of another directory without being in it?

Here is what my directory looks like:
Test ----
|
|----One
|
|----Two
I am attempting to list the contents of Two while still being in One and I am not able to do so.
I have tried this command (as seen in a other post) : "ls Test/" and it says No such file or directory. I have also tried ls Test/Two/ and it still does not work.
If you are in One and you want to list the contents of Two, you should go up to the parent directory using ..:
ls ../Two
../ will place you in the Test directory, from there, you can go to Two with no problem. If you have more depth levels, just add more ../ to go up one directory each time, but mind which is your current directory when running the command.
what does this command provide you in terminal.
ls -al Test //a flag is used for hidden file

Makefile rule depend on directory content changes

Using Make is there a nice way to depend on a directories contents.
Essentially I have some generated code which the application code depends on. The generated code only needs to change if the contents of a directory changes, not necessarily if the files within change their content. So if a file is removed or added or renamed I need the rule to run.
My first thought is generate a text file listing of the directory and diff that with the last listing. A change means rerun the build. I think I will have to pass off the generate and diff part to a bash script.
I am hoping somehow in their infinite intelligence might have an easier solution.
Kudos to gjulianm who got me on the right track. His solution works perfect for a single directory.
To get it working recursively I did the following.
ASSET_DIRS = $(shell find ../../assets/ -type d)
ASSET_FILES = $(shell find ../../assets/ -type f -name '*')
codegen: ../../assets/ $(ASSET_DIRS) $(ASSET_FILES)
generate-my-code
It appears now any changes to the directory or files (add, delete, rename, modify) will cause this rule to run. There is likely some issue with file names here (spaces might cause issues).
Let's say your directory is called dir, then this makefile will do what you want:
FILES = $(wildcard dir/*)
codegen: dir # Add $(FILES) here if you want the rule to run on file changes too.
generate-my-code
As the comment says, you can also add the FILES variable if you want the code to depend on file contents too.
A disadvantage of having the rule depend on a directory is that any change to that directory will cause the rule to be out-of-date — including creating generated files in that directory. So unless you segregate source and target files into different directories, the rule will trigger on every make.
Here is an alternative approach that allows you to specify a subset of files for which additions, deletions, and changes are relevant. Suppose for example that only *.foo files are relevant.
# replace indentation with tabs if copy-pasting
.PHONY: codegen
codegen:
find . -name '*.foo' |sort >.filelist.new
diff .filelist.current .filelist.new || cp -f .filelist.new .filelist.current
rm -f .filelist.new
$(MAKE) generate
generate: .filelist.current $(shell cat .filelist.current)
generate-my-code
.PHONY: clean
clean:
rm -f .filelist.*
The second line in the codegen rule ensures that .filelist.current is only modified when the list of relevant files changes, avoiding false-positive triggering of the generate rule.

Rename many images with names from file

I have a file with a list of names. Let's call it nameFile. For example:
John Doe
John Benjamin
Benjamin Franklin
...
I also have a folder of pictures. The pictures are named like:
pic001.jpg
pic002.jpg
pic003.jpg
...
I want to rename each picture with the corresponding name from the nameFile. Thus, pic001.jpg will become 'John Doe.jpg', pic002.jpg will become 'John Benjamin.jpg', etc.
Is there an easy UNIX command to do this? I know mv can be used to rename, I'm just a bit unsure how to apply it to this situation.
Mostly people do it by writing a simple shell script.
These two links will help you to do it.
Bulk renaming of files in unix
Rename a group of files with one command
The mv is a Unix command that renames one or more files or directories. The original filename or directory name is no longer accessible. Write permission is required on all directories and files being modified.
mv command syntax
You need to use the mv command to rename a file as follows:
mv old-file-name new-file-name
mv file1 file2
mv source target
mv [options] source target

Resources