How to get a bare, recursive directory listing in Linux, excluding some directories - linux

I need to obtain a recursive directory listing in Linux with only the directory and file name. It needs to include all files including hidden files with the exception of files name “.svn”.
I have tried multiple combinations of the “ls” command and haven’t been able to figure it out. When using “ls –R direname/ grep –v /$” I get a directory heading followed by a colon, which I cannot use.
If I have a directory name test with files and a sub-directory named test2 with files, I need the output to look like the following:
test
test/.filehidden1
test/file2
test/file3.txt
test/test2.log
test/test2/file.hidden1
test/test2/file2.boo
test/test2/file3.boo2
Notice there is no leading forward slash

find . -name .svn -prune -o -print
-prune tells it to not descend into any matching directories.

This should do:
find . ! -path \*.svn\*
This tells find to recursively list all files from . whose pathname does not contains .svn. This is not perfect since it may hide for instance file foo.svnbar.

Something like this:
find DIRNAME ! -name .svn

I need to obtain a recursive directory listing in Linux with only the directory and file name. It needs to include all files including hidden files with the exception of files name “.svn”.
Do you want to get a list of files in a Subversion repository?
This will do just that:
svn ls -R
If not, then you can use find:
find . ! -path '*/.svn/*' ! -name .svn
You mentioned "Notice there is no leading forward slash".
If that's very important, then the find command can be rephrased as:
find * .[^.]* ! -path '.svn/*' ! -name .svn

Related

Linux find command explanation

Can someone explain me what does this command do and if I want to try the same thing using git, how should I modify this command?
find . -name CVS -print -exec rm -fr {} \;
This command looks in your current working directory for any directories or files named "CVS" and prints the full path. Then executes a forced recursive removal for each result returned by the find command.
Since there is no filetype present in the name, this command will remove any directory, within your current working directory, named CVS, including all subdirectories and files housed within.

How to remove all files except the ones that matches a pattern along several number of directories

lets say I have a my_dirs/ directory, insdie that directory I have several parallel subdirectories which has several files and I want to delete all of them except the ones that have the substring '.regions'
this is my parent directory content:
this is what I tried:
shopt -s extglob
rm -r !(./**/*.regions*)
but I got an error message: cannot be deleted «! (./**/*. region *) »: The file or directory does not exist.
how can I do that?
First of all, always be careful when deleting multiple files.
The command to achieve what you want would be:
find my_dirs -type f ! -name "*.regions*" -delete
"-delete" must be last, otherwise it will delete everything it finds
This will explore all subdirectories in my_dirs, find the files (-type f) that not (!) contain ".regions" ("*.regions*") on their name, and delete (-delete) them.
I recommend running this first: find my_dirs -type f ! -name "*.regions*",
so it won't delete anything and you can check the files are correct.
Edit: Added -type f so it only targets files per Philippe's suggestion.

Recursive find that will append directory name to any file

Any help would be greatly appreciated!
I need a way to append the parent directory name to any file in any path.
An example current directory tree
/Hawaii/Surfing/800x600/picture1.jpg
/Hawaii/Surfing/800x600/picture2.jpg
/Hawaii/Surfing/800x600/picture3.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture1.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture2.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture3.jpg
The goal
/Hawaii/Surfing/800x600/picture1.800x600.jpg
/Hawaii/Surfing/800x600/picture2.800x600.jpg
/Hawaii/Surfing/800x600/picture3.800x600.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture1.4096x2160.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture2.4096x2160.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture3.4096x2160.jpg
I have found some examples of this but the users all have set directory depths unfortunately I have files at many different levels.
find dir -name *.jpg -exec rename -nv -- 's|/(.*)/(.*)$|/$1/$1.jpg|' {}
Your first capture group is matching everything before the last /, not just the last directory name. Use /([^/]*)/ instead of /(.*)/ so it won't match across / delimiters. You're also not splitting up the filename into the name and extension, so you're not inserting the directory name between them.
find dir -name *.jpg -exec rename -nv -- 's|([^/]*)/([^/]*)\.jpg$|$1/$2.$1.jpg|' {} +

Search recursively for files in a parent directory in Linux

I am trying to list all the files in a parent directory and its subdirectories. However, I am running this command from another location. So, at first, I need to traverse to the directory (from where I want to run this command).
Please note that I am using the find command instead of ls because I also want to list down the absolute path for each file in front of it. This is not possible with the ls command.
here is what I am doing:
cd ../../../;cd level1_dir1;find $(pwd) . -name *.* -printf "%TY-%Tm-%Td\t%p\n"
This command does not show any output.
Here is the directory structure:
level1_dir1
this has multiple subdirectories:
level2_dir1
level2_dir2
....
level2_dir10
each of these subdirectories again have subdirectories and files inside them.
however, now if I do:
cd ../../../;cd level1_dir1/level2_dir1;find $(pwd) . -name *.* -printf "%TY-%Tm-%Td\t%p\n"
it will do the recursion properly for all the subdirectories in level2_dir1 and show output like:
date level1_dir1/level2_dir1/path/to/file/filename
so, I wanted to print out for all the level2 directories, this way (by using the wild character):
cd ../../../;cd level1_dir1/*;find $(pwd) . -name *.* -printf "%TY-%Tm-%Td\t%p\n"
but it prints out the results only for the first directory in level2 (that is level2_dir1)
how can I make it list down the files for all the subdirectories?
thanks.
How about this?
find ../../../level1_dir1 -printf "%TY-%Tm-%Td\t%p\n"
If you want all the files, you don't even need -name in the find command. If you don't want to see the directories and only the files, just add "-type f" before -printf.
Hope this helps...

Bash script to recursively step through folders and delete files

Can anyone give me a bash script or one line command i can run on linux to recursively go through each folder from the current folder and delete all files or directories starting with '._'?
Change directory to the root directory you want (or change . to the directory) and execute:
find . -name "._*" -print0 | xargs -0 rm -rf
xargs allows you to pass several parameters to a single command, so it will be faster than using the find -exec syntax. Also, you can run this once without the | to view the files it will delete, make sure it is safe.
find . -name '._*' -exec rm -Rf {} \;
I've had a similar problem a while ago (I assume you are trying to clean up a drive that was connected to a Mac which saves a lot of these files), so I wrote a simple python script which deletes these and other useless files; maybe it will be useful to you:
http://github.com/houbysoft/short/blob/master/tidy
find /path -name "._*" -exec rm -fr "{}" +;
Instead of deleting the AppleDouble files, you could merge them with the corresponding files. You can use dot_clean.
dot_clean -- Merge ._* files with corresponding native files.
For each dir, dot_clean recursively merges all ._* files with their corresponding native files according to the rules specified with the given arguments. By default, if there is an attribute on the native file that is also present in the ._ file, the most recent attribute will be used.
If no operands are given, a usage message is output. If more than one directory is given, directories are merged in the order in which they are specified.
Because dot_clean works recursively by default, use:
dot_clean <directory>
If you want to turn off the recursively merge, use -f for flat merge.
dot_clean -f <directory>
find . -name '.*' -delete
A bit shorter and perform better in case of extremely long list of files.

Resources