LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Better Practice When Working With Files (https://www.linuxquestions.org/questions/linux-newbie-8/better-practice-when-working-with-files-4175419652/)

random0munky 07-31-2012 09:32 PM

Better Practice When Working With Files
 
Hey,

I have a question that I ran into when working with files on my linux box. I went ahead and pasted what I have in terminal.

MacBookPro:Test random0munky$ ls -l
total 0
-rw-r--r-- 1 random0munky staff 0 Jul 31 19:31 myTest
-rw-r--r-- 1 random0munky staff 0 Jul 31 19:31 myTest.new
MacBookPro:Test random0munky$ mv myTest myTest.orig
MacBookPro:Test random0munky$ mv myTest.new myTest
MacBookPro:Test random0munky$ ls -l
total 0
-rw-r--r-- 1 random0munky staff 0 Jul 31 19:31 myTest
-rw-r--r-- 1 random0munky staff 0 Jul 31 19:31 myTest.orig
MacBookPro:Test random0munky$

I feel there's a better way / using better practices to accomplish the same task. What do you guys think?

Edit: I noticed that when you do the mv command, that the permissions as well as the group doesn't update with the file. Now just need to see about how to go about updating those 2 values.

evo2 07-31-2012 09:56 PM

Hi,

how about
Code:

mv -b -S '.orig' myTest.new myTest
If you do this often and also want to preserve the perms I would suggest a little shell script, shell function or alias.

Evo2.

random0munky 07-31-2012 10:02 PM

Quote:

Originally Posted by evo2 (Post 4742671)
Hi,

how about
Code:

mv -b -S '.orig' myTest.new myTest
If you do this often and also want to preserve the perms I would suggest a little shell script, shell function or alias.

Evo2.

I can see where you're going with this. Unfortunately, the permissions didn't transfer =/

Edit: How about this: mv -b -S '.orig' myTest.new myTest | chmod --reference myTest.orig myTest

evo2 07-31-2012 10:57 PM

Hi,
Quote:

Originally Posted by random0munky (Post 4742678)
Edit: How about this: mv -b -S '.orig' myTest.new myTest | chmod --reference myTest.orig myTest

Sure. You could put the following in your .bashrc
Code:

mvbk () {
mv -b -S '.orig' $1 $2 && chmod --reference $2.orig $2
}

Then you just need to call
Code:

mvbk myTest.new myTest
You could also modify the function so that you it only needs one argument assuming that the you will always be wanting to move $1.new to $1
eg
Code:

mvbk () {
mv -b -S '.orig' $1.new $1 && chmod --reference $1.orig $1
}

Then you could just call
Code:

mvbk myTest
Evo2.

random0munky 07-31-2012 11:02 PM

Quote:

Originally Posted by evo2 (Post 4742716)
Hi,

Sure. You could put the following in your .bashrc
Code:

mvbk () {
mv -b -S '.orig' $1 $2 && chmod --reference $2.orig $2
}

Then you just need to call
Code:

mvbk myTest.new myTest
You could also modify the function so that you it only needs one argument assuming that the you will always be wanting to move $1.new to $1
eg
Code:

mvbk () {
mv -b -S '.orig' $1.new $1 && chmod --reference $1.orig $1
}

Then you could just call
Code:

mvbk myTest
Evo2.

Is there any other way that's a bit more prettier than the solutions we have came up with >.<

evo2 07-31-2012 11:17 PM

Hi,
Quote:

Originally Posted by random0munky (Post 4742718)
Is there any other way that's a bit more prettier than the solutions we have came up with >.<

Hmm. Don't know. I like little shell functions.

Cheers,

Nick.


All times are GMT -5. The time now is 02:26 AM.