How to check permissions of a specific directory? - unix

I know that using ls -l "directory/directory/filename" tells me the permissions of a file. How do I do the same on a directory?
I could obviously use ls -l on the directory higher in the hierarchy and then just scroll till I find it but it's such a pain. If I use ls -l on the actual directory, it gives the permissions/information of the files inside of it, and not of the actual directory.
I tried this in the terminal of both Mac OS X 10.5 and Linux (Ubuntu Gutsy Gibbon), and it's the same result. Is there some sort of flag I should be using?

Here is the short answer:
$ ls -ld directory
Here's what it does:
-d, --directory
list directory entries instead of contents, and do not dereference symbolic links
You might be interested in manpages. That's where all people in here get their nice answers from.
refer to online man pages

You can also use the stat command if you want detailed information on a file/directory. (I precise this as you say you are learning ^^)

$ ls -ld directory
ls is the list command.
- indicates the beginning of the command options.
l asks for a long list which includes the permissions.
d indicates that the list should concern the named directory itself; not its contents. If no directory name is given, the list output will pertain to the current directory.

In GNU/Linux, try to use ls, namei, getfacl, stat.
For Dir
[flying#lempstacker ~]$ ls -ldh /tmp
drwxrwxrwt. 23 root root 4.0K Nov 8 15:41 /tmp
[flying#lempstacker ~]$ namei -l /tmp
f: /tmp
dr-xr-xr-x root root /
drwxrwxrwt root root tmp
[flying#lempstacker ~]$ getfacl /tmp
getfacl: Removing leading '/' from absolute path names
# file: tmp
# owner: root
# group: root
# flags: --t
user::rwx
group::rwx
other::rwx
[flying#lempstacker ~]$
or
[flying#lempstacker ~]$ stat -c "%a" /tmp
1777
[flying#lempstacker ~]$ stat -c "%n %a" /tmp
/tmp 1777
[flying#lempstacker ~]$ stat -c "%A" /tmp
drwxrwxrwt
[flying#lempstacker ~]$ stat -c "%n %A" /tmp
/tmp drwxrwxrwt
[flying#lempstacker ~]$
For file
[flying#lempstacker ~]$ ls -lh /tmp/anaconda.log
-rw-r--r-- 1 root root 0 Nov 8 08:31 /tmp/anaconda.log
[flying#lempstacker ~]$ namei -l /tmp/anaconda.log
f: /tmp/anaconda.log
dr-xr-xr-x root root /
drwxrwxrwt root root tmp
-rw-r--r-- root root anaconda.log
[flying#lempstacker ~]$ getfacl /tmp/anaconda.log
getfacl: Removing leading '/' from absolute path names
# file: tmp/anaconda.log
# owner: root
# group: root
user::rw-
group::r--
other::r--
[flying#lempstacker ~]$
or
[flying#lempstacker ~]$ stat -c "%a" /tmp/anaconda.log
644
[flying#lempstacker ~]$ stat -c "%n %a" /tmp/anaconda.log
/tmp/anaconda.log 644
[flying#lempstacker ~]$ stat -c "%A" /tmp/anaconda.log
-rw-r--r--
[flying#lempstacker ~]$ stat -c "%n %A" /tmp/anaconda.log
/tmp/anaconda.log -rw-r--r--
[flying#lempstacker ~]$

There is also
getfacl /directory/directory/
which includes ACL
A good introduction on Linux ACL here

This displays files with its permisions
stat -c '%a - %n' directory/*

In addition to the above posts, i'd like to point out that "man ls" will give you a nice manual about the "ls" ( List " command.
Also, using ls -la myFile will list & show all the facts about that file.

On OS X you can use:
ls -lead
The e option shows ACLs. And ACLs are very important to knowing what the exact permissions on your system are.

ls -lstr
This shows the normal ls view with permissions and user:group as well

To check the permission configuration of a file, use the command:
ls –l [file_name]
To check the permission configuration of a directory, use the command:
ls –l [Directory-name]

Related

Using xargs to create a remote directory

I want to use the xargs command to read the standard output from my date command. The following pipe works, creating a directory "2019-12-03" in the current directory.
date "+%Y-%m-%d" -r ../IDNumber/IDNumber.txt | xargs mkdir
What I would like it to do, however, is use the standard output from the data command to make a directory in a remote location, username#archivalstorage.university.edu:/remote/folder/path/, resulting in username#archivalstorage.university.edu:/remote/folder/path/2019-12-03.
Running the following command:
date "+%Y-%m-%d" -r ../IDNumber/IDNumber.txt | xargs mkdir -p username#archivalstorage.university.edu:/remote/folder/path/
This command does not give any kind of error, but no folder is actually created in the remote location I have specified.
mkdir doesn't support the remote directory. Use ssh instead, with command substitute:
ssh user#host 'cd /path/to/dir && mkdir `date +%Y-%m-%d`'
or with xargs:
date +%Y-%m-%d | xargs -I _ ssh user#host 'cd /tmp && mkdir _'

Using wildcard with symlink command creates wrong links when run from Jenkins

I can't wrap my head around this issue. I run symlink command over SSH from my Jenkins job. In my Jenkins pipeline I have these 2 steps (among others).
sh "ssh ubuntu#${host} sudo ls -al /etc/nginx/sites-available"
sh "ssh ubuntu#${host} sudo ln -sv /etc/nginx/sites-available/* /etc/nginx/sites-enabled/ -f"
Here's the relevant part of the log:
[Pipeline] sh
[my_job] Running shell script
+ ssh ubuntu#XX.XX.XX.XX sudo ls -al /etc/nginx/sites-available
total 12
drwxr-xr-x 2 root root 4096 Sep 18 17:27 .
drwxr-xr-x 6 root root 4096 Aug 30 12:27 ..
-rw-r--r-- 1 ubuntu ubuntu 467 Sep 18 17:27 my-nginx-config
[Pipeline] sh
[my_job] Running shell script
+ ssh ubuntu#XX.XX.XX.XX sudo ln -sv /etc/nginx/sites-available/default /etc/nginx/sites-available/jenkins /etc/nginx/sites-enabled/ -f
'/etc/nginx/sites-enabled/default' -> '/etc/nginx/sites-available/default'
'/etc/nginx/sites-enabled/jenkins' -> '/etc/nginx/sites-available/jenkins'
Symlinks are created on my remote host but instead of the my-nginx-config symlink, the default and jenkins files (which are on my jenkins host at /etc/nginx/sites-available) are symlinked.
If I don't use wildcard and run this, it works as expected:
sh "ssh ubuntu#${host} sudo ls -al /etc/nginx/sites-available"
sh "ssh ubuntu#${host} sudo ln -sv /etc/nginx/sites-available/my-nginx-conf /etc/nginx/sites-enabled/ -f"
Sometimes remote ssh commands (and locations for scp) need extra escaping. I know you've already quoted your query, but you may need one additional level of escapes (and because the escape would be interpreted by your double-quotes rather than being passed to the SSH command, you need one more.
Try double-escaping that wildcard:
sh "ssh ubuntu#${host} sudo ls -al /etc/nginx/sites-available"
sh "ssh ubuntu#${host} sudo ln -sv /etc/nginx/sites-available/\\* /etc/nginx/sites-enabled/ -f"
You can also combine those into one SSH call:
sh "ssh ubuntu#${host} sudo ls -al /etc/nginx/sites-available; sudo ln -sv /etc/nginx/sites-available/\\* /etc/nginx/sites-enabled/ -f"
I'm not a Jenkins expert. If this doesn't work, I'd try yet another pair of escapes, changing your original …/sites-available/* to …/sites-available/\\\\*
The remote commands need to be quoted (although most of them work without quotes):
sh "ssh ubuntu#${host} 'sudo ln -sv /etc/nginx/sites-available/* /etc/nginx/sites-enabled/ -f'"

Unix Copy Recursive Including All Directories

I have the following two directories:
~/A
drawable/
imageb.png
new/`
newimage.png
~/B
drawable/
imagec.png
When I use the cp -r ~/A/* ~/B command newimage.png with its new/ folder is copied across to ~/B however imageb.png is not copied into ~/B/drawable.
Could you explain why this is the case and how I can get around this?
Use tar instead of cp:
(cd A ; tar cf - *) | (cd B ; tar xf -)
or more compactly (if you're using GNU tar):
tar cC A -f - . | tar xC B -f -
If you are on linux you can use the -r option.
eg: cp -r ~/A/. ~/B/
If you are on BSD you could use the -R option.
eg: cp -R ~/A/. ~/B/
For more information on exactly what option you should pass, refer man cp
Also note that, if you do not have permissions to the file you it would prevent copying files.

Exception handling app/logs/dev.log

The stream or file "/home/nick/projects/Symfony/app/logs/dev.log" could not
be opened: failed to open stream: Permission denied
I get it in symfony.
I know that I can go to app/logs and delete dev.log but this is happening to often and is annoying to delete it again and againand again.
Is there any way to make it just disapear?
LE: I guess, that I got the reply.
I run the comands prefixed with sudo.
Eg: sudo php app/console generate:bundle --namespace=Acme/StoreBundle will work like a charm.
nick#ptb:~/projects/Symfony$ ls -al app/logs
total 76
drwxrwxrwx+ 2 nick nick 4096 nov 24 15:16 .
drwxr-xr-x 6 nick nick 4096 nov 24 15:28 ..
-rw-rw-r--+ 1 www-data www-data 59583 nov 24 16:25 dev.log
-rw-rw-r--+ 1 nick nick 0 oct 6 15:55 .gitkeep
I think that you create directory app/logs as sudo (root), so symfony doesn't have permissions to write into the file dev.log. Manually remove directory app/logs (and all contents), create it as normal user and setup up permissions, like they are described in the official documentation http://symfony.com/doc/current/book/installation.html.
Something like:
$ sudo rm -rf app/logs
$ mkdir app/logs
$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
$ sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
$ sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
Or use any other option which is described in official documentation.
If this not works then please paste us result of command:
$ ls -al app/logs
In my case, I used CentOS 7 and I solved it by set SELINUX=disabled as follow.
- sudo nano /etc/selinux/config
- Edit the file by set SELINUX=disabled
- Restart again then it worked !
I tested and tried many method included chmod, chwon, setfacl, etc but it cannot solve the problem until I tried above solution and it worked!
You should have a look at this http://symfony.com/doc/current/book/installation.html#configuration-and-setup.
UPD
Sometimes the solutions using ACL won't work, in this case try the forth option 4. Use the same user for the CLI and the web server
Use commands prefixed with sudo. I also got this error when I tried to run the server. Then I used 'sudo' and the error disappeared.
$ sudo php bin/console server:run

Can you change what a symlink points to after it is created?

Does any operating system provide a mechanism (system call — not command line program) to change the pathname referenced by a symbolic link (symlink) — other than by unlinking the old one and creating a new one?
The POSIX standard does not. Solaris 10 does not. MacOS X 10.5 (Leopard) does not. (I'm tolerably certain neither AIX nor HP-UX does either. Judging from this list of Linux system calls, Linux does not have such a system call either.)
Is there anything that does?
(I'm expecting that the answer is "No".)
Since proving a negative is hard, let's reorganize the question.
If you know that some (Unix-like) operating system not already listed has no system call for rewriting the value of a symlink (the string returned by readlink()) without removing the old symlink and creating a new one, please add it — or them — in an answer.
Yes, you can!
$ ln -sfn source_file_or_directory_name softlink_name
AFAIK, no, you can't. You have to remove it and recreate it. Actually, you can overwrite a symlink and thus update the pathname referenced by it:
$ ln -s .bashrc test
$ ls -al test
lrwxrwxrwx 1 pascal pascal 7 2009-09-23 17:12 test -> .bashrc
$ ln -s .profile test
ln: creating symbolic link `test': File exists
$ ln -s -f .profile test
$ ls -al test
lrwxrwxrwx 1 pascal pascal 8 2009-09-23 17:12 test -> .profile
EDIT: As the OP pointed out in a comment, using the --force option will make ln perform a system call to unlink() before symlink(). Below, the output of strace on my linux box proving it:
$ strace -o /tmp/output.txt ln -s -f .bash_aliases test
$ grep -C3 ^unlink /tmp/output.txt
lstat64("test", {st_mode=S_IFLNK|0777, st_size=7, ...}) = 0
stat64(".bash_aliases", {st_mode=S_IFREG|0644, st_size=2043, ...}) = 0
symlink(".bash_aliases", "test") = -1 EEXIST (File exists)
unlink("test") = 0
symlink(".bash_aliases", "test") = 0
close(0) = 0
close(1) = 0
So I guess the final answer is "no".
EDIT: The following is copied from Arto Bendiken's answer over on unix.stackexchange.com, circa 2016.
This can indeed be done atomically with rename(2), by first creating the new symlink under a temporary name and then cleanly overwriting the old symlink in one go. As the man page states:
If newpath refers to a symbolic link the link will be overwritten.
In the shell, you would do this with mv -T as follows:
$ mkdir a b
$ ln -s a z
$ ln -s b z.new
$ mv -T z.new z
You can strace that last command to make sure it is indeed using rename(2) under the hood:
$ strace mv -T z.new z
lstat64("z.new", {st_mode=S_IFLNK|0777, st_size=1, ...}) = 0
lstat64("z", {st_mode=S_IFLNK|0777, st_size=1, ...}) = 0
rename("z.new", "z") = 0
Note that in the above, both mv -T and strace are Linux-specific.
On FreeBSD, use mv -h alternately.
Editor's note: This is how Capistrano has done it for years now, ever since ~2.15. See this pull request.
It is not necessary to explicitly unlink the old symlink. You can do this:
ln -s newtarget temp
mv temp mylink
(or use the equivalent symlink and rename calls). This is better than explicitly unlinking because rename is atomic, so you can be assured that the link will always point to either the old or new target. However this will not reuse the original inode.
On some filesystems, the target of the symlink is stored in the inode itself (in place of the block list) if it is short enough; this is determined at the time it is created.
Regarding the assertion that the actual owner and group are immaterial, symlink(7) on Linux says that there is a case where it is significant:
The owner and group of an existing symbolic link can be changed using
lchown(2). The only time that the ownership of a symbolic link matters is
when the link is being removed or renamed in a directory that has the sticky
bit set (see stat(2)).
The last access and last modification timestamps of a symbolic link can be
changed using utimensat(2) or lutimes(3).
On Linux, the permissions of a symbolic link are not used in any operations;
the permissions are always 0777 (read, write, and execute for all user
categories), and can't be changed.
Just a warning to the correct answers above:
Using the -f / --force Method provides a risk to lose the file if you mix up source and target:
mbucher#server2:~/test$ ls -la
total 11448
drwxr-xr-x 2 mbucher www-data 4096 May 25 15:27 .
drwxr-xr-x 18 mbucher www-data 4096 May 25 15:13 ..
-rw-r--r-- 1 mbucher www-data 4109466 May 25 15:26 data.tar.gz
-rw-r--r-- 1 mbucher www-data 7582480 May 25 15:27 otherdata.tar.gz
lrwxrwxrwx 1 mbucher www-data 11 May 25 15:26 thesymlink -> data.tar.gz
mbucher#server2:~/test$
mbucher#server2:~/test$ ln -s -f thesymlink otherdata.tar.gz
mbucher#server2:~/test$
mbucher#server2:~/test$ ls -la
total 4028
drwxr-xr-x 2 mbucher www-data 4096 May 25 15:28 .
drwxr-xr-x 18 mbucher www-data 4096 May 25 15:13 ..
-rw-r--r-- 1 mbucher www-data 4109466 May 25 15:26 data.tar.gz
lrwxrwxrwx 1 mbucher www-data 10 May 25 15:28 otherdata.tar.gz -> thesymlink
lrwxrwxrwx 1 mbucher www-data 11 May 25 15:26 thesymlink -> data.tar.gz
Of course this is intended, but usually mistakes occur. So, deleting and rebuilding the symlink is a bit more work but also a bit saver:
mbucher#server2:~/test$ rm thesymlink && ln -s thesymlink otherdata.tar.gz
ln: creating symbolic link `otherdata.tar.gz': File exists
which at least keeps my file.
Wouldn't unlinking it and creating the new one do the same thing in the end anyway?
Just in case it helps: there is a way to edit a symlink with midnight commander (mc).
The menu command is (in French on my mc interface):
Fichier / Éditer le lien symbolique
which may be translated to:
File / Edit symbolic link
The shortcut is C-x C-s
Maybe it internally uses the ln --force command, I don't know.
Now, I'm trying to find a way to edit a whole lot of symlinks at once (that's how I arrived here).
Technically, there's no built-in command to edit an existing symbolic link. It can be easily achieved with a few short commands.
Here's a little bash/zsh function I wrote to update an existing symbolic link:
# -----------------------------------------
# Edit an existing symbolic link
#
# #1 = Name of symbolic link to edit
# #2 = Full destination path to update existing symlink with
# -----------------------------------------
function edit-symlink () {
if [ -z "$1" ]; then
echo "Name of symbolic link you would like to edit:"
read LINK
else
LINK="$1"
fi
LINKTMP="$LINK-tmp"
if [ -z "$2" ]; then
echo "Full destination path to update existing symlink with:"
read DEST
else
DEST="$2"
fi
ln -s $DEST $LINKTMP
rm $LINK
mv $LINKTMP $LINK
printf "Updated $LINK to point to new destination -> $DEST"
}
You can modify the softlink created once in one of the two ways as below in Linux
one is where you can remove existing softlink with rm and again create new softlink with ln -s command .
However this can be done in one step , you can replace existing softlink with updated path with "ln -vfns Source_path Destination_path" command.
Listing initial all files in directory
$ ls -lrt
drwxrwxr-x. 3 root root 110 Feb 27 18:58 test_script
$
Create softlink test for test_script with ln -s command.
$ ln -s test_script test
$ ls -lrt
drwxrwxr-x. 3 root root 110 Feb 27 18:58 test_script
lrwxrwxrwx. 1 root root 11 Feb 27 18:58 test -> test_script
$
Update softlink test with new directory test_script/softlink with single command
$ ln -vfns test_script/softlink/ test
'test' -> 'test_script/softlink/'
$
List new softlink location
$ ls -lrt
lrwxrwxrwx. 1 root root 21 Feb 27 18:59 test -> test_script/softlink/
$
ln --help
-v, --verbose print name of each linked file
-f, --force remove existing destination files
-n, --no-dereference treat LINK_NAME as a normal file if it is a symbol
-s, --symbolic make symbolic links instead of hard links

Resources