Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Safer file manipulation in the Terminal UNIX
By default in OSX, terminal commands like move (mv), copy (cp) or remove (rm) can overwrite or delete existing files without prompting you whether it's really want you want to do. These commands can be dangerous to use especially when using * for designing multiple files. To be automatically prompted for confirmation before each file is processed, create yourself a .cshrc file in your home directory and put the following lines
alias mv 'mv -i'
alias rm 'rm -i'
alias cp 'cp -i'
Another thing I find very useful is to have the target directory listed automatically when issuing a cd command. This can be done as well by adding the following line in your .cshrc file:
alias cd  'cd \!*;echo $cwd; ls -FC'
For these changes to be effective, type source .cshrc or open a new terminal window.

[Editor's note: Please see this related conversation on aliases in another macosxhints' posting. Aliases can live in a number of locations; .tcsh is one of them, but the referenced article gives an alternate, (possibly better?) location for these types of files.]
    •    
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[3,009 views]  

Safer file manipulation in the Terminal | 5 comments | Create New Account
Click here to return to the 'Safer file manipulation in the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Potential danger
Authored by: danielj7 on May 15, '01 09:17:30PM
This can be useful, but if you come to rely on it, it can be disastrous. What happens if you are using another computer or even a different account on the same computer? You'll type "rm *" expecting it to ask you if you want to delete each file, but instead it'll just silently delete them. A better alternative would be

alias rmi rm -i

or

alias del rm -i

Another thing that can make life easier, if you're using tcsh, is to use

set rmstar

in your ".cshrc" file. Now if you type "rm *" the shell will ask if you really want to delete all files. A good source for such hints is Using csh & tcsh from O'Reilly.

[ Reply to This | # ]
Warning
Authored by: Bart VdBroeck on May 16, '01 12:34:38PM
That should be:


alias cd 'cd \!*;echo $cwd; ls -FC'


Yes, you guessed it, the famous GeekLog bug ate the backslash again. Putting in the command without the backslash somehow prevented my .cshrc file from being loaded.

[ Reply to This | # ]
Sigh...
Authored by: robg on May 16, '01 12:42:30PM

Good catch, Bart. I'm hoping the next geeklog release fixes this bug. For now, if you need a backslash, you need to use the HTML character code (\) AND then paste it again after you 'preview' your comment -- preview converts it to an actual backslash, which will get dropped.

I've dug into the text manipulation code myself, but I'm not enough of a PHP/mySQL wizard to figure out what's going on ... anyone care to take a look? Send me an email and I'll send you the PHP snippets that deal with text handling...

-rob.



[ Reply to This | # ]
Fixed the hint...
Authored by: robg on May 16, '01 12:43:55PM

I just added the backslash to the original hint, so it's now correct...



[ Reply to This | # ]
OSX is over-organized
Authored by: groue on Oct 08, '01 10:44:37AM

... Have you ever read discussions about name of localhost ?
It's so hard learning that /etc/hosts is replaced by NetInfo...
Well, if you want to secure tcsh, here is a better way do to it.

If you stare at tcsh starting up process, you can see in
/usr/share/init/tcsh all startup scripts. If you edit them,
you can see that .mine files are automatically loaded, for all
users or only for one !!

That's why I added in /usr/share/init/tcsh (for ALL users)
the files :

----- start of aliases.mine file
##
# Default Aliases add-ons
# Gw on 23/09/01
##

#
# some secure aliases missing in /usr/share/init/tcsh/aliases
#

# ask before removing
alias rm 'rm -i'

# ask before replacing
alias mv 'mv -i'
alias cp 'cp -i'

#
# some usage aliases
#

# display logical path instead of physical path.
# Use /bin/pwd for physical path
alias pwd 'pwd -L'

# less instead of more
alias more less

# clear screen
alias c screen

# make ll to quit automatically when EOF is reached
alias ll 'ls -lag !* | less -E'
----- end of aliases.mine file

I also added some usefull stuff in the same directory

----- start of environment.mine
##
# Default Environment add-ons
# Gw on 23/09/01
##

# replace more by less for man command
setenv PAGER less

# less options
setenv LESS "-eMr"
----- end of environment.mine

And finally a different completion behavior, because I like to type
nothing except the first letters.

----- start of completion.mine
##
# completion add-ons
# Gw on 23/09/2001
##

# tab expand completion
# Use ^D for displaying list of matches
bindkey "^I" complete-word-fwd
----- end of completion.mine

Hope this helps :o)
Gwendal Roué (with a é hu ?)



[ Reply to This | # ]