shell command to extract the part of filename having characters? [duplicate] - linux

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
In Bash, how to strip out all numbers in the file names in a directory while leaving the file extension intact
(1 answer)
Closed 5 years ago.
I have a file named(multi_extension123.txt). Before copying this file into the directory I need to remove files like
[multi_extension1234.txt
multi_extension1234.txt
multi_extension12345.txt] if present in the directory and then copy the earlier one to this directory. Can anyone give the solution with shellscript.
note: i need to remove only numerical numbers and extension alone.
I have tried this
$ filename= $file1
$ echo "${filename%[0-9].*}"
find . -type f maxdepth 0 mindepth 0 -name "'$filename'[0-9]*.txt" -exec rm -f {} \;

Related

How to get deleted files into a log file. Bash script [duplicate]

This question already has answers here:
Linux find and delete files but redirect file names to be deleted
(5 answers)
Closed 1 year ago.
So im using the script
find /path/to/files/* -mtime +60 -exec rm {} \;
How can i collect the deleted files and transfer them into a logfile in Bash script
You could do something like:
find /path/... -print ... | tee -a <log.file>
The -print will print out all the hits, and the tee will append that to some log.file.
Side note: the * at the end of your /path/to/files/* seems superfluous.
Side note2: if you just want to delete the files, find has a built-in -delete.

How can I search for specific file contents in all files in both current folder and all subfolders [duplicate]

This question already has answers here:
How do I recursively grep all directories and subdirectories?
(26 answers)
Closed 4 years ago.
Using Mac terminal server linux/bash commands, how can I search for a particular text string in all *.txt files in the current folder plus all files in the subfolders inside the current folder?
grep -i "xxx" */*
xxx is the target text I am trying to find.
find . -type f -print | egrep ".txt$" | xargs grep "SearchPatern"
Explained as
Find all the file names in the current directory and below send to ....
grep which picks out the file name that end in .txt and send names to ....
xargs which will execute a grep command on each file to look for SearchPatern.

How do I concatenate file contents into a single file in bash? [duplicate]

This question already has answers here:
Concatenating multiple text files into a single file in Bash
(12 answers)
Closed 4 years ago.
I have files in different directories under a parent directory, something like this:
Parent Dir
Dir 1 - File 1
Dir 2 - File 2
I want to have an output file that appends the content of File1 with File2. How do I do it in Bash?
Converting my comment to an answer. You can use following find + xargs pipeline command:
cd /parent/dir
find home -name "*.orc" -print0 | xargs -0 cat > test.orc

Search recursively all files with a given name replacing a word [duplicate]

This question already has answers here:
BASH: recursive program to replace text in a tree of files
(3 answers)
Closed 5 years ago.
From a given folder, I want to search recursively accross all subfolders searching for files with name file.txt replacing all occurences of Foo -case sensitive-
with Bar.
Which is the simplest way of achieving this with basic scripting (Bash / sed / grep / find...).
find + sed solution:
find . -type f -name "file.txt" -exec sed -i 's/Foo/Bar/g' {} \;

how to list full paths of folders inside a directory in linux? [duplicate]

This question already has answers here:
Show full path when using options
(8 answers)
Closed 8 years ago.
I have a folder /home/Documents/myFolder and inside this folder there are lots other folders. I want to have a file list.txt which contains all the paths of the folders. I want the text file content like this:
/home/Documents/myFolder/1234
/home/Documents/myFolder/asd2
/home/Documents/myFolder/asdawgf
/home/Documents/myFolder/dawt
.
.
.
I tried this one but it was not what I want ls > /home/Documents/myFolde/list.txt
it just prints the folder names. I want the full paths.
Use find listing all directories (-type d) and then sed the output to get the full path correct:
find . -type d | sed -n 's:^\./:/home/Documents/myFolder/:'p > /home/Documents/myFolder/list.txt
you can use find:
find . > /home/Documents/myFolde/list.txt

Resources