How do i find all file Which as name String+Number to it Linux - linux

I have to find all files whose name start with String + Number like below
ABC123_filedemo.txt
AB_451_filetxt
CD_789_demo.txt
demo_files_FD123.txt
d_files_re_SD_456.txt
I have tried this Command But Not working
export _date=`date "+%d_%m_%Y_%H_%M_%S"`
find . -type f -iname 'AB*' -exec mv {} /Demo_files"_"$_date \;

Is ".txt" relevant? Then try:
find ./ -regextype posix-extended -regex '.*[a-zA-Z]+.*[0-9]+\.txt' -exec <your stuff>
inspired by: https://stackoverflow.com/a/5249797/10514446

This looks like a simple example:
find ./ -name "[a-zA-Z]*[0-9]*"
Every file here needs to start with a letter 'a'-'z' (small or large caps) and somewhere in the filename you need a digit [0-9].

Related

How to rename files in different directories with the same name using find

I have files named test.txt in different directories like this
./222/test.txt
./111/test.txt
I want to rename all test.txt to info.txt
I've tried using this
find . -type f -iname 'test.txt' -exec mv {} {}info \;
I get test.txtinfo
Your idea is right, but you need to use -execdir instead of just -exec to simplify this.
find . -type f -iname 'test.txt' -execdir mv {} info.txt ';'
This works like -exec with the difference that the given shell command is executed with the directory of the found pathname as its current working directory and that {} will contain the basename of the found pathname without its path. Also note that the option is a non-standard one (non POSIX compliant).

Remove files in subdirectories older than 1 day with Linux command

I am honestly nowhere near to be a decent bash scripter, but I made a little research and found a command that seems to be useful
find /path/to/files* -mtime +1 -exec rm {} \;
The question is if this line will remove directories? Because I want to only remove files that are images (actually in a *.jpeg format)
No, rm without the -r flag does not remove directories.
It looks like you want to add some more filters:
-type f to match only files
-name '*.jpeg' to match only files ending with .jpeg
Lastly, instead of -exec rm {} \;, you could use the much simpler -delete.
Putting it together, this looks more appropriate for you:
find /path/to/files* -mtime +1 -type f -name '*.jpeg' -delete
Then narrow your search results to *.jpeg files:
find /path/to/files* -mtime +1 -type f -name "*.jpeg" -exec rm {} \;
It's always better to remove the exec parameter to do a dry run before delete:
find /path/to/files* -mtime +1 -type f -name "*.jpeg"
Each line will be passed to rm command, and nothing more.

Linux : Remove/Delete .txt files ending with exactly five digits

BTW this command worked for me- rm {path}/*[0-9][0-9][0-9][0-9][0-9].txt
Is there a shorter way? Because as per need we could extend this to
delete all .txt files ending with exactly 10 digits.
--We need to delete files having names like abc12345, ac12456, abcd98653 and so on..
This cmd finds and rm's files with the filename pattern [any number of digits].txt in linux:
find /path/to/search -type f -regextype posix-extended -regex '^.*/[0-9]+\.txt' -exec rm -f {} \;
And this one rm's files with exactly 10 digits.
find /path/to/search -type f -regextype posix-extended -regex '^.*/[0-9]{10}\.txt' -exec rm -f {} \;
Try it without the "-exec rm -f {} \;" bit first to see if it matches the correct files to delete

How to find all files with a filename that ends with tilde

I use Emacs and it sometimes makes backup for edited files. After a few days, I would have a lot of backup files whose name ends with a tilde.
Is there a way to find these files and delete them at once?
I tried this:
find "*" -type f -iname *~
But it doesn't work. I want the command to work recursively – something like ls -alR.
You need to escape from the shell. And you need to specify search path, not *
find . -type f -name '*~'
To delete the files:
find . -type f -name '*~' -exec rm -f '{}' \;
You can do something like that :
find . -type f -name '*~' -delete
If you want to delete also #*# file :
find . -type f -name '*~' -o -name '#*#' -delete
You can print all deleted files with "-print":
find . -type f -name '*~' -delete -print
Another way is by using grep.
lnydex99uhc:javastuff user$ ls
Permutation.java VigenereCipher.java VigenereCipher.java~
lnydex99uhc:javastuff user $ find . | grep .~$
./VigenereCipher.java~
You can also pass any command you want like this :
lnydex99uhc:javastuff zatef$ rm $(find . | grep .~$)

In Unix,cmd to search a file recursively and retrieve the file instead of just the path of the file

In Unix, what is the single cmd that lets me search and locate a file recursively and then retrieve the file instead of just the path of the file?
What do you mean by retrieve?
You can simply use -exec argument to find.
$ find /path/to/search -type f -name '*.txt' -exec cat {} \;
$ find /path/to/search -type f -name 'pattern' -exec cp {} /path/to/new \;
The second one should work.
cat `find /wherever/you/want/to/start/from -name name_of_file`
Note those quotes are backquotes (`).

Resources