05-27-2011
Thanks for the pointer:-
After lot of drafts, this is the content of final script:-
------------
This will find all directories named .git, and make sure their parent directories have names ending with .git:
Code:
find / -depth -type d -name .git -printf '%h\0' | while read -d "" old ; do
new="${old%.git}.git"
[ ! -d "$old" ] && continue
[ "$old" == "$new" ] && continue
if [ -e "$new" ]; then
echo "$new: Already exists" >&2
else
mv -vi "$old" "$new"
fi
done
The -depth flag will tell find to descend first, so that all child directories are checked before a parent directory. The -printf '%h\0' option will output the parent directory name and a NUL separator (zero byte; the only thing you cannot have in pathnames). If using Bash, the -d "" option to the read builtin will read such NUL-separated pathnames; other shells won't support that.
(If you are not using Bash, use -printf "%h\n" | while read dir ; do instead. It'll bork if you have directories with a newline in their names, though.)
The first continue test checks if this directory has already been renamed something else. (I think it is impossible for that to happen, but it doesn't hurt to check.)
The second continue test checks if the directory already has an acceptable name.
If the new directory name is still available, the directory is renamed.
_____________________________________________
This will find empty all directories that have names ending in .git (but not just .git), and create file "file" in it:
Code:
find / -depth -type d -name '?*.git' -printf '%p\0' | while read -d "" dir ; do
find "$dir/" -depth -mindepth 1 -delete
touch "$dir/file"
done
The glob pattern ?*.git matches (anything not empty).git .
The inner find deletes everything in the directory, but not the directory itself (since minimum depth is 1). Although rm usually feels like the choice for this, it'd either delete the directory itself too, skip any files or subdirectories having a dot in the name, or throw an error about nonexistent files, depending on what you supply to it. If you delete and then recreate the directory, you'll lose any extended attributes. So, surprisingly, find is better for this.
-----------------
---------- Post updated at 02:24 AM ---------- Previous update was at 01:48 AM ----------
Just a quick doubt here:-
Now i would like to perform 1 more operation here:-
1. Traverse entire file structure and search for all the directories which has .git directory present in it...
2. In successful cases, go inside those directories and perform one operation:-
x = /home/myproject/mystuff/testing (some common path which will be used in all cases)
Objective:- <some command> $x/immediate_dir_name.git
I am expecting there will be 80-90 successful scenarios where the .git directory will be present inside the parent, now within each of those directories, i would like to perform some command on a common path ($x) followed by original_name_of_the_parent appended by .git
For ex:-
if there is a dir with name testing having .git, i would like to execute command inside testing directory
<some command> $x/testing.git
Will this work?? Is this the right way of doing it...
----------
x = /home/myproject/mystuff/testing
find $PWD -name '.git' -type d | while read -d "" dir ; do
<some command> "$dir/ $x/$dir.git"
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Hi all
Is it possible to copy a structure of a directory only.
e.g.
I have a file with the following entries that is a result of a find :-
/dir1/dir2/file.dbf
/dir1/dir2/dir3/file1.dbf
/dir1/file.dbf
I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies
2. UNIX for Dummies Questions & Answers
I have written this code, and according to my research it SHOULD be going down the list until it is finished, but I am getting blank feedback. Nothing is being output as far as I can tell.
#!/bin/sh
while echo Enter to start traversing
read enter
do
read list <&3
echo $list
done
any... (2 Replies)
Discussion started by: MaestroRage
2 Replies
3. Programming
Hello Groups
I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records.
if we loop it in both the structures it is going to consume time.
I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies
4. Shell Programming and Scripting
hi experts(novice people can stay away as it is no child's game),
i am developing a script which works like recycle bin of windows.
the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to... (1 Reply)
Discussion started by: yahoo!
1 Replies
5. Shell Programming and Scripting
Started a thread in beginners but I thought someone from this forum may have an idea as well. Thanks!
EDITED BY REBORG (3 Replies)
Discussion started by: robotsbite
3 Replies
6. Shell Programming and Scripting
How do I write a bash or ruby or perl or groovy script to print all the files in my directory tree that are one-to-two years old, the size of each file, and the sum of file sizes and then delete them?
I was using
find . -atime +365 -exec rm '{}' \;
but the problem was that I could not... (5 Replies)
Discussion started by: siegfried
5 Replies
7. AIX
If I have a file/folder that a user does not have permission to and I try to rename it, it removes the entire parent folder. At that point it is only visible in a ls. (Not a ls -l, file, more, cd). It happens on every filesystem. This is Aix 5.3
$ cd test
$ ls -la
total 0
drwxr-xr-x 2 root ... (4 Replies)
Discussion started by: scriptr2be
4 Replies
8. Shell Programming and Scripting
I have to write a script to rename the every last sub-directory in a directory structure if the last sub-directory name doesn't contain "submitted".
eg:
given directory path:/u01/home/somedir
somedir can have many subdirectories and each subdirectory inturn has many subdirectories.
somedir... (3 Replies)
Discussion started by: ramse8pc
3 Replies
9. Shell Programming and Scripting
Hi All,
I have a directory like this: /u01/app/oracle/11gSE1/11gR203
How do i get the top level directory /u01 from this? Tried dirname and basename but dint help. I can this using echo $ORACLE_HOME | awk -F"/" '{print "/"$2}'. But I am trying to find out if there is a better way of doing it... (4 Replies)
Discussion started by: nilayasundar
4 Replies
10. UNIX for Beginners Questions & Answers
Hi All,
Just started learning unix and stuck into below issue.
Suppose i have folder structure as below.
Dir1/Dir2/Dir3/File1.msg
I am looking to rename the file name from File1.msg to File2.msg but from the parent Dir1
From Dir3 i can easily run the command like
mv File1.msg... (2 Replies)
Discussion started by: Gurjeet Singh
2 Replies
LEARN ABOUT DEBIAN
git-mv
GIT-MV(1) Git Manual GIT-MV(1)
NAME
git-mv - Move or rename a file, a directory, or a symlink
SYNOPSIS
git mv <options>... <args>...
DESCRIPTION
This script is used to move or rename a file, directory or symlink.
git mv [-v] [-f] [-n] [-k] <source> <destination>
git mv [-v] [-f] [-n] [-k] <source> ... <destination directory>
In the first form, it renames <source>, which must exist and be either a file, symlink or directory, to <destination>. In the second form,
the last argument has to be an existing directory; the given sources will be moved into this directory.
The index is updated after successful completion, but the change must still be committed.
OPTIONS
-f, --force
Force renaming or moving of a file even if the target exists
-k
Skip move or rename actions which would lead to an error condition. An error happens when a source is neither existing nor controlled
by GIT, or when it would overwrite an existing file unless -f is given.
-n, --dry-run
Do nothing; only show what would happen
-v, --verbose
Report the names of files as they are moved.
GIT
Part of the git(1) suite
Git 1.7.10.4 11/24/2012 GIT-MV(1)