Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 5857  / 3 Years ago, wed, august 4, 2021, 5:46:17

one thing that always drove me crazy is the need to do



 mv file1 tmp
mv file2 file1
mv tmp file2


in oder to swap 2 filenames.



what do you guys use to swap 2 files? Are there any standard script for swaping?
something like sw file1 file2 would be nice


More From » scripts

 Answers
1

Suggestion would be to add to bash a function that will swap two files:



nano .bashrc


paste in at the bottom of the file:



function swap()         
{
if [ $# -ne 2 ]; then
echo "Usage: swap file1 file2"
else
local TMPFILE=$(mktemp)
mv "$1" $TMPFILE
mv "$2" "$1"
mv $TMPFILE "$2"
fi
}


either logout/login or run:



source .bashrc


To swap two files run:



swap x y


where x and y are your file-names



source


[#42724] Thursday, August 5, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hentor

Total Points: 482
Total Questions: 104
Total Answers: 111

Location: South Korea
Member since Sun, Dec 25, 2022
1 Year ago
;