This question already has answers here:
Removing part of a filename for multiple files on Linux
(7 answers)
Closed 4 years ago.
I have multiple files in a folder and files name format like below
12.xyz.dat.cache
13.abc.dat.cache
I have to rename above files like below
12.xyz.dat
13.abc.dat
Basically I have to remove tailing '.cache' Can you provide the command.
I tried below, did not work.
rename 's/\.dat\.cache$/\.dat' *.dat.cache
Thanks, Mahdu
This is because you missed the trailing slash '/' in the perlexp. And to be sure to catch a '.dat' file, you should escape the .:
rename 's/\.dat.cache$/\.dat/' *.dat.cache
Try this one.
If you don't have or don't like rename, you can use this following alternative for remove all trailing .cache :
find . -name '*.cache' | while read NAME; do mv "${NAME}" "${NAME%.cache}"; done
Related
This question already has answers here:
Rename multiple files based on pattern in Unix
(24 answers)
Closed 2 years ago.
I have a lot of file with the same format (???_ideal.sdf) name and I need to rename all (???.sdf) removing form the name "ideal". For example:
Files:
002_ideal.sdf
ERT_ideal.sdf
234_ideal.sdf
sCX_idel.sdf
New Files:
002.sdf
ERT.sdf
234.sdf
SCX.sdf
I thought to using a loop but I don’t know how to indicate that in the new file name should be removed "individual".
For example:
for file in ???_individual.sdf; mv $file what?
With your shown samples, could you please try following. This will only print the rename command on terminal(for safer side, check and make sure if commands are fine and looking ok to you first before actually renaming files), to perform actual rename remove echo from following.
for file in *_ideal.sdf
do
echo mv "$file" "${file/_ideal/}"
done
Based on this answer you can also write it as one line with:
rename 's/^(.*)_ideal.png/$1.png/s' **/**
First parameter replaces the the _ideal.png, second one means all files.
This question already has an answer here:
In bash, how do I expand a wildcard while it's inside double quotes?
(1 answer)
Closed 4 years ago.
I am trying to add a line to a specific file which matches a pattern in its name.
e.g. I am trying to append text STATUS PASSED to a file whose name contains 2018_09_26_04_51_30.
date="2018_09_26_04_51_30"
echo "STATUS PASSED" >> "/test_dir/*$date*.txt"
Above said commands are creating new file named *2018_09_26_04_51_30*.txt which is not serving my purpose!
Let's assume that there are several other files with *.txt extension, but none of these files contains $date in their names.
The test_dir directory contains:
test1-2018_09_26_04_50_48.txt
test2-2018_09_26_04_50_56.txt
test3-2018_09_26_04_51_03.txt
test3-2018_09_26_04_51_30_51S.txt
So, here file test3-2018_09_26_04_51_30_51S.txt is unique.
P.S. I have to execute this script in both Linux and AIX.
Any help will be appreciated!
Try like this,
date="2018_09_26_04_51_30"
#cd /test_dir
echo "STATUS PASSED" >> $(ls /test_dir/*$date*.txt)
This question already has answers here:
Get just the filename from a path in a Bash script [duplicate]
(6 answers)
Closed 4 years ago.
Let's say I have a string which represents the full path of a file:
full_path='./aa/bb/cc/tt.txt'.
How can I extract only the file name tt.txt?
Please don't tell me to use echo $full_path | cut -d'/' -f 5.
Because the file may be located in a deeper or shallower folder.
The number, 5, cannot be applied in all cases.
if you are comfortable with python then, you can use the following piece of code.
full_path = "path to your file name"
filename = full_path.split('/')[-1]
print(filename)
Use the parameter expansion functionality.
full_path='./aa/bb/cc/tt.txt'
echo ${full_path%/*}
That will give you the output
./aa/bb/cc
And will work any number of directory levels deep, as it will give you everything up to the final "/".
Edit: Parameter expansion is very useful, so here's a quick link for you that gives a good overview of what is possible: http://wiki.bash-hackers.org/syntax/pe
You can use this construct
echo "$full_path" | sed 's/\/.*\///'
This question already has answers here:
sed edit file in place
(15 answers)
Closed 7 years ago.
I want to remove the headers of a file and replace its content without headers in the same file.
Example: file_student
name age
XYS 24
RTF 56
The output should be:
XYS 24
RTF 56
The scenario is that I do not want to create any new file for this change. Can sed do this?
I tried:
sed 1d /tmp/file_student.txt |
hadoop fs -copyfromLocal /tmp/file_student.txt /tmp/file_student_no_header.txt
But that does not work. Any help is appreciated!
an extract from sed's man page
-i[SUFFIX]'
--in-place[=SUFFIX]'
This option specifies that files are to be edited in-place. GNU
`sed' does this by creating a temporary file and sending output to
this file rather than to the standard output.(1).
This option implies `-s'.
When the end of the file is reached, the temporary file is renamed
to the output file's original name. The extension, if supplied,
is used to modify the name of the old file before renaming the
temporary file, thereby making a backup copy(2)).
This rule is followed: if the extension doesn't contain a `*',
then it is appended to the end of the current filename as a
suffix; if the extension does contain one or more `*' characters,
then _each_ asterisk is replaced with the current filename. This
allows you to add a prefix to the backup file, instead of (or in
addition to) a suffix, or even to place backup copies of the
original files into another directory (provided the directory
already exists).
If no extension is supplied, the original file is overwritten
without making a backup.
so you need to change your sed command to sth like
sed -i 1d file | whatever
hope this helps.
If you don't want to use sed, try tail - e.g. you have a file called xxx:
tail -n +2 xxx > xxx.tmp && mv xxx.tmp xxx
This question already has answers here:
Getting the parent of a directory in Bash
(13 answers)
Closed 1 year ago.
In my script I need the directory of the file I am working with. For example, the file="stuff/backup/file.zip". I need a way to get the string "stuff/backup/" from the variable $file.
dirname $file
is what you are looking for
dirname $file
will output
stuff/backup
which is the opposite of basename:
basename $file
would output
file.zip
Using ${file%/*} like suggested by Urvin/LuFFy is technically better since you won't rely on an external command. To get the basename in the same way you could do ${file##*/}. It's unnecessary to use an external command unless you need to.
file="/stuff/backup/file.zip"
filename=${1##*/} # file.zip
directory=${1%/*} # /stuff/backup
It would also be fully POSIX compliant this way. Hope it helps! :-)
For getting directorypath from the filepath:
file="stuff/backup/file.zip"
dirPath=${file%/*}/
echo ${dirPath}
Simply use $ dirname /home/~username/stuff/backup/file.zip
It will return /home/~username/stuff/backup/