Should I put trailing slash after source and destination when copy folders
I try to put a trailing /
on the target.
If I make a mistake and the source and target are both files rather than directories, adding the /
means that I'll get an error; without the /
it will clobber the target file.
(Tab completion will usually add the trailing /
anyway, so it's usually easier to add it than not.)
See Volker Siegel's answer for more details.
linux: which is the right way to copy folder?
With cp
, if the destination directory already exists and you do not use a trailing slash on the source-dir, then you are actually putting a copy of source-dir inside dest-dir; this can be a problem when you forgot that the destination directory already exists.
You should include the trailing slash, to make it obvious to cp
that you are trying to copy a directory name to a new directory name, and not copy the directory into an existing one, if it exists.
PowerShell Copy-Item: do I need a backslash at the end of my destination parameter?
Your coworker might be confusing PowerShell with VBScript. VBScript's CopyFile
and CopyFolder
methods require a trailing backslash if the destination is a folder. PowerShell's Copy-Item
cmdlet doesn't care whether the destination does or doesn't have a trailing backslash as long as the folder exists.
He's not entirely wrong, though. If the destination folder doesn't exist a trailing backslash in the destination path does make a difference:
PS C:\Temp> ls -r | select Mode, FullName | ft -AutoSize
Mode FullName
---- --------
d---- C:\Temp\a
-a--- C:\Temp\a\foo.txt
PS C:\Temp> Copy-Item 'C:\Temp\a\foo.txt' 'C:\temp\b'
PS C:\Temp> ls -r | select Mode, FullName | ft -AutoSize
Mode FullName
---- --------
d---- C:\Temp\a
-a--- C:\Temp\b
-a--- C:\Temp\a\foo.txt
PS C:\Temp> Remove-Item 'C:\temp\b'
PS C:\Temp> Copy-Item 'C:\Temp\a\foo.txt' 'C:\temp\b\'
Copy-Item : The filename, directory name, or volume label syntax is incorrect.
...
A non-existing destination path with a trailing backslash throws an error, whereas without a trailing backslash the destination is created as a file.
However, IMO a better way to deal with missing destination folders is to actually check for their presence and create them if they don't exist:
if (-not (Test-Path -LiteralPath $destination -Type Container)) {
New-Item -Type Directory -Path $destination | Out-Null
}
Slashes and the rsync command
It's described in the rsync(1)
manpage:
A trailing slash on the source changes this behavior to avoid creating an additional directory level at the destination. You can think of a
trailing/
on a source as meaning "copy the contents of this directory" as opposed to "copy the directory by name", but in both cases the
attributes of the containing directory are transferred to the containing directory on the destination. In other words, each of the following commands copies the files in the same way, including their setting of the attributes of/dest/foo
:rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo
As to the destination, I don't think it has any major consequences. There is a difference if the source is a file and destination doesn't exist — this will make a copy of SRC
called DEST
:
rsync SRC DEST
, whereas, this will create directory DEST
and copy the SRC
file into it:
rsync SRC DEST/
Should a directory path variable end with a trailing slash?
I don't include the trailing slash when I, for example, define a directory for storing files. That is because I will use it like
$store_file = "$store_path/$file_id";
I will always add a trailing slash before using a variable that's supposed to hold a directory path. I think it's better to always add one than to wonder if the trailing slash is included.
What is the simplest way to remove a trailing slash from each parameter?
You can use the ${parameter%word}
expansion that is detailed here. Here is a simple test script that demonstrates the behavior:
#!/bin/bash
# Call this as:
# ./test.sh one/ two/ three/
#
# Output:
# one two three
echo ${@%/}
When should I use a trailing slash in my URL?
In my personal opinion trailing slashes are misused.
Basically the URL format came from the same UNIX format of files and folders, later on, on DOS systems, and finally, adapted for the web.
A typical URL for this book on a Unix-like operating system would be a file path such as file:///home/username/RomeoAndJuliet.pdf, identifying the electronic book saved in a file on a local hard disk.
Source: Wikipedia: Uniform Resource Identifier
Another good source to read: Wikipedia: URI Scheme
According to RFC 1738, which defined URLs in 1994, when resources contain references to other resources, they can use relative links to define the location of the second resource as if to say, "in the same place as this one except with the following relative path". It went on to say that such relative URLs are dependent on the original URL containing a hierarchical structure against which the relative link is based, and that the ftp, http,
and file URL schemes are examples of some that can be considered hierarchical, with the components of the hierarchy being separated by "/".
Source: Wikipedia Uniform Resource Locator (URL)
Also:
That is the question we hear often. Onward to the answers! Historically, it’s common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to
denote a file:http://example.com/foo/ (with trailing slash, conventionally a directory)
http://example.com/foo (without trailing slash, conventionally a file)
Source: Google WebMaster Central Blog - To slash or not to slash
Finally:
A slash at the end of the URL makes the address look "pretty".
A URL without a slash at the end and without an extension looks somewhat "weird".
You will never name your CSS file (for example) http://www.sample.com/stylesheet/ would you?
BUT I'm being a proponent of web best practices regardless of the environment.
It can be wonky and unclear, just as you said about the URL with no ext.
How do I copy folder with files to another folder in Unix/Linux?
The option you're looking for is -R
.
cp -R path_to_source path_to_destination/
- If
destination
doesn't exist, it will be created. -R
meanscopy directories recursively
. You can also use-r
since it's case-insensitive.- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use
-a
flag along with trailing/.
in the source (as per@muni764
's /@Anton Krug
's comment):
cp -a path_to_source/. path_to_destination/
What did Rsync Delete?
I believe you have pasted the "previous" command exactly the same as the "new" one but I will assume the previous one did not have the trailing slash.
When you put a trailing slash at the end of a source directory, rsync treats it as a "contents of this directory". So if you do "rsync pics/ otherdir" you put contents of pics into otherdir. If you do "rsync pics otherdir", you would put the directory pics into otherdir.
In the destination directory there is no difference whether you put trailing slash or not. If the directory doesn't exist, it will get created. Depending on the existance of trailing slash after source directory, it will either have contents of source in it or the source directory in it.
Example:
rsync -avv /media/drive/backup/ /media/drive2/backup
is the same as
rsync -avv /media/drive/backup /media/drive2
Related Topics
What Exactly Is Sudo Bang Bang
How to Change File Permissions in Ubuntu
Best Distributed Filesystem for Commodity Linux Storage Farm
Shell Init Issue When Click Tab, What's Wrong with Getcwd
Sanitize Environment with Command or Bash Script
Does Linux Kill Background Processes If We Close the Terminal from Which It Has Started
How I Should Run My Golang Process in Background
Difference Between Checkout and Export in Svn
Android Studio Error After ./Studio.Sh
Setting Environment Variable Globally Without Restarting Ubuntu
How the Util of iOStat Is Computed
How to Check Syslog in Bash on Linux
How to Tell Which Unix Shell I am Using
Remove the Last Page of a PDF File Using PDFtk