LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2015, 09:36 PM   #1
RobInRockCity
Member
 
Registered: Feb 2015
Posts: 141

Rep: Reputation: Disabled
Trailing Slash or Not?


When I type in a path to a directory - whether it be in SSH or a config file or wherever - should I end it with a trailing slash or not?

For example...
Code:
/home/rob123/temp/
or

Code:
/home/rob123/temp

Rob
 
Old 02-26-2015, 11:22 PM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
I really don't think it matters, commands like 'cd' work either way.

FWIW: using bash, if I use tab to complete a path, it adds the trailing slash. So, if you want to be sure you're operating on a directory, I would add it.
 
Old 02-26-2015, 11:31 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
It doesn't matter for most things, as long as it is unambiguous that it is a directory.

It does matter when using rsync however... from man rsync:

Quote:
...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 direc-
tory" 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.
With bash autocomplete, presence or absence of a tailing slash added by the shell will allow you to visually distinguish between a directory and a symlink to a directory. This behavior can also vary with inputrc directive show-all-if-ambiguous, I think.

It is usually important when used in config files and varies per application.

Last edited by astrogeek; 02-26-2015 at 11:32 PM.
 
Old 02-27-2015, 09:50 AM   #4
RobInRockCity
Member
 
Registered: Feb 2015
Posts: 141

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by goumba View Post
So, if you want to be sure you're operating on a directory, I would add it.
That has been the approach I have taken so far.

Just wasn't sure, because when I look at some things online or even my php.ini file, I have seen it both ways.

I prefer to be "precise"!!


Rob
 
Old 02-27-2015, 09:51 AM   #5
RobInRockCity
Member
 
Registered: Feb 2015
Posts: 141

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
It doesn't matter for most things, as long as it is unambiguous that it is a directory.

It does matter when using rsync however... from man rsync:
Quote:
...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 direc-
tory" 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.
Interesting point!

Thanks,


Rob
 
Old 02-27-2015, 10:11 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
If you leave off the trailing slash, it can point to a file or a directory. If you include the trailing slash it can only point to a directory. This can be advantageous in certain cases. For example, say you want to copy A (a file) into B (a directory).

You could use either
Code:
cp A B
or
Code:
cp A B/
If B already exists and is a directory, the two commands are interchangeable, but if B doesn't already exist, or if it does already exist and is a file, the behavior will be quite different.

The former command will just copy the file A to the file B, overwriting its previous contents if necessary.

The latter command will ONLY do anything if B already exists and is a directory. If B doesn't exist or if B is a file, the latter command will throw an error, allowing you to correct the command before accidentally overwriting the file B or creating copies of A where you don't want them.

For that reason, when I'm referring to a directory, I always put a trailing slash on the end so that if I make a mistake in the name it will throw an error rather doing something I don't want it to do.

Last edited by suicidaleggroll; 02-27-2015 at 10:13 AM.
 
3 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add trailing slash adrianc.grigoras Linux - Software 1 05-13-2009 06:26 AM
Apache2 trailing slash issues! mephitic Linux - Software 2 02-04-2005 03:24 AM
Apache trailing slash boondock_saint Linux - General 9 07-07-2004 03:31 PM
Apache trailing slash puzzler tantric Linux - Software 4 04-11-2004 04:18 PM
Necessary trailing slash in apache directories silversky Linux - Software 3 03-03-2004 03:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration