Reputation: 5329
I have a requirement where I need to copy some files from one location to other (Where the file may exist). While doing so,
I am facing problem in point 2. While I am trying to get the destination path for copying files, I am unable to extract the directory of the file. I tried using various options of find
command, but was unable to crack it.
I need to trim the file name from the full file path so that it can be used in cp
command. I am new to shell scripting. Any pointers are appreciated.
Upvotes: 0
Views: 98
Reputation: 26
If you need only the filename, why not do a
basename /root/wkdir/index.txt
and assign it to a variable which would return only the filename?
Upvotes: 0
Reputation: 69208
You can use
cp --backup
-b'
--backup[=METHOD]' *Note Backup options::. Make a backup of each file that would otherwise be overwritten or removed. As a special case, `cp' makes a backup of SOURCE when the force and backup options are given and SOURCE and DEST are the same name for an existing, regular file. One useful application of this combination of options is this tiny Bourne shell script:#!/bin/sh # Usage: backup FILE... # Create a GNU-style backup of each listed FILE. for i; do cp --backup --force -- "$i" "$i" done
Upvotes: 2