This question already has answers here:
Moving files to a directory
(6 answers)
Closed 4 years ago.
Would anyone know how to move a file to the very end of a file path? I have tried looking this up but haven’t gotten anywhere. What I’m trying to do is:
mv filename.txt dir1/dir2/dir3/dir4/
I don’t want to list out every directory. Any way to just send it all the way to the end? Any advice or help would be appreciated.
If I understood you, you just need a shell variable to save you from having to repeatedly type out a long path, thus:
p=dir1/dir2/dir3/dir4/
mv filename1.txt $p
mv filename2.txt $p
...
And if you need the $p variable to be always set, you can define it in your .bash_profile or .bashrc file.
Related
This question already has answers here:
Recursively change file extensions in Bash
(6 answers)
Closed 29 days ago.
I'm on linux Mint. I'd like to recursively rename files like
5f0c74603cbdca44fd877_source.mp4?Tag=1&Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiaHR0cHM6XC9cL2NkbjIub25seWZhbnMuY29tXC9maWxlc1wvOVwvOWNcLzljNTQzZGI1OGI0ZmQyYWI0YmExMzEzMTUxYmExZjdiXC81ZjBjNzQ2MDNjYmRjYTQ0ZmQ4Nzdfc291cmNlLm1wND9UYWc9MS
1904x2600_8c25949033674d6559bcfd3f02aed68d.jpg?Tag=1&Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiaHR0cHM6XC9cL2NkbjIub25seWZhbnMuY29tXC9maWxlc1wvYVwvYTFcL2ExNjZmMzRlZGZjMWU3NDRkOGIxZTEwYmZkNDIxNzJjXC8xOTA0eDI2MDBfOGMyNTk0OTAzMzY3NGQ2NTU5
to
5f0c74603cbdca44fd877_source.mp4
1904x2600_8c25949033674d6559bcfd3f02aed68d.jpg
so that I can copy files to an external hard drive.
Anyone know a quick one liner in bash that could accomplish this?
There's also another type of file that cannot be moved
index.html?C=D;O=D
How can I rename that to something that has valid characters?
Thanks!
I see you search to remove all characters after first ? (included).
for f in *\?*; do mv "$f" "${f%%\?*}"; done
This question already has answers here:
Renaming part of a filename [duplicate]
(6 answers)
Closed 1 year ago.
I need to write a bash script.
I have a folder name abc and the folder contains multiple (around 20) files. Now some of the files are named as __servicename__List.java Here __servicename is acting as a placeholder. I need to replace this placeholder with a string say xyz
Can someone please help?
Appreciate all your help! Thanks in advance!
In bash I would do:
cd abc
for FileName in **/* ; do
mv -- "$FileName" "${FileName/__servicename__/xyz}"
done
This question already has answers here:
Bash script to find and display oldest file
(4 answers)
Closed 2 years ago.
I'm looking for a BASH command (or set of commands) that will look in a specific directory and delete ONLY the single oldest file in that directory. I've looked around, but I can't quite find what I'm looking for. Hopefully someone can help me with this, because it's the last missing piece in my script. Everything else is working perfectly.
One way to delete oldest file ending with .specific :
rm -i $(ls -tr *.specific | sed q)
This is not very reliable if you have spaces in filenames
This question already has answers here:
"~/Desktop/test.txt: No such file or directory"
(2 answers)
Why isn't tilde (~) expanding inside double quotes? [duplicate]
(1 answer)
Closed 4 years ago.
I'm new to Linux as well as bash. I made a script that accesses a folder that is located in the home directory, but the script will not always be called from the home directory. The prompt I'm getting when calling it from any subdirectories specifies that it can not find the file.
Here's an example of what I'm trying to do:
for entry in "~/.directory"/*
do
echo "$entry"
done
If I place the script in a subdirectory of /home and try to call it, the script is unable to find the directory. I know it exists as if I run ls ~/.directory in the subdirectory it is able to find the files and print them with no problem. Is there a different way I should be trying to access the directory in the bash shell? Thanks!
Voted to close my question. It seems rather specific to me, and the general solution was something I found earlier and was also posted in the comments below. I'll figure it out eventually -
Only unquoted tildes are expanded.
for entry in ~/".directory"/*
This question already has answers here:
How to loop over directories in Linux?
(11 answers)
Closed 7 years ago.
I'm not sure how to handle directories of directory scenario in shell.
I have folder structure as below.
Directory structure:
/DirA/DirA1/DirA11/*.txt
/DirA2/DirA21/*.txt
/DirA3/DIrA31/*.txt'
I'm new to shell scripting, not able to figure out how to read these text files.
You can use the find command to process all files with certain properties in a directory tree. For example,
find /DirA* -name '*.txt' 2>/dev/null
would list all files named *.txt inside the trees you are mentioning. Note that if you use wildcards in the name mask, you need to single-quote them in order to protect them from the shell.
for f in /DirA/DirA1/DirA11/*.txt /DirA2/DirA21/*.txt /DirA3/DIrA31/*.txt; do
# do stuff with $f
done