How to compress a symbolic link? - linux

With tar, I tried to compress a repository in a chroot environment with ssh, but it failed as it just compressed the symbolic link itself.
How can I compress its content?
-bash-3.00$ ls -ail
1263583 lrwxrwxrwx 1 root root 18 Apr 18 2008 mainwebsite_html -> ../../var/www/html
-bash-3.00$ tar -cjvf archive.tar.bz2 mainwebsite_html
-bash-3.00$ ls -ail
7509101 -rw-r--r-- 1 user1 user1 136 Feb 22 08:57 archive.tar.bz2
1263583 lrwxrwxrwx 1 root root 18 Apr 18 2008 mainwebsite_html -> ../../var/www/html

From the tar(1) man page:
-h, --dereference
follow symlinks; archive and dump the files they point to

Take care of placing the "h" option in the correct place. In your example:
tar -cjvf archive.tar.bz2 mainwebsite_html
should be
tar -chjvf archive.tar.bz2 mainwebsite_html

Related

permission denied when renaming file in linux at /var/www/html

I am very new to linux and am using putty and pscp to move files to a live hosted directory. I would like to rename my recently uploaded dist folder to its actual project name using mv but get a permission denied error. What is the safest and most reasonable way to arrange these files so I can rename them?
lawton#lawtonb:/var/www/html$ ls -la
total 268
drwxr-xr-x 4 root root 4096 Aug 30 16:42 .
drwxr-xr-x 3 root root 4096 Aug 18 23:05 ..
-rw-rw-r-- 1 lawton lawton 253276 Aug 27 18:06 bundle.js
drwxr-xr-x 2 root root 4096 Aug 30 16:42 dist
-rw-r--r-- 1 root root 17 Aug 18 23:24 info.php
drwxr-xr-x 2 root root 4096 Aug 27 18:59 react-youtube
lawton#lawtonb:/var/www/html$ mv dist BTC_Sorter
mv: cannot move 'dist' to 'BTC_Sorter': Permission denied
If this is a one-time rename, your best course of action would be to run mv with sudo, like this:
sudo mv dist BTC_Sorter
If, however, you'd like to have permissions for your user or group, you should do some reading about chown (The link belongs to the Arch Linux wiki, but should be useful regardless of your distro).
In your particular case, the directory dist is owned both by the user and the group root, hence why you'd need to use sudo, chown or simply the root account.
You are not the owner of the file, you need execute the command with sudo
sudo mv src dest

Applying chmod 755 (or other) when ftping to apache var/www/ virtual host directories?

I'm struggling to understand how to apply directory and file permissions so that files and directories created by the ftp user, via an ftp client, are browsable. In other words 755 is applied by default.
I'm running Apache 2 on Ubuntu 14.04.2 LTS, Trusty Tahr, with vsftpd server.
My directory structure is as follows.
var/www/
var/www/sites/
var/www/sites/master_demo/public_html
var/www/sites/test_site/public_html
I've run the following
usermod -aG www-data test_user
chgrp -R www-data /var/www/sites/test_site/public_html/
chown -R test_site:www-data /var/www/sites/test_site/public_html/
chmod -R 755 /var/www/sites/test_site/public_html/
Permissions on the directories are as follows:
cd var/www/sites/
ls -l
dr-xrws--- 3 master_demo www-data 4096 Jun 17 14:00 master_demo
drwxr-xr-x 3 test_site www-data 4096 Jun 16 13:32 test_site
cd var/www/sites/test_site/
ls -l
drwxr-xr-x 3 test_site www-data 4096 Jun 17 17:43 public_html
When a file is added using ftp by the test_site user these are the permissions
-rw------- 1 test_site test_site 152 Jun 18 08:27 about.html
drwx------ 2 test_site test_site 4096 Jun 18 08:27 css
-rw------- 1 test_site test_site 152 Jun 18 08:27 index.html
How do I get the file to automatically be given the correct permissions so the files are browsable (755)? I know the obvious answer might be to run a batch file, but is that necessary? I don't want to have to run the command everytime.
chmod -R 755 /var/www/sites/test_site/public_html/*
ls -l
-rwxr-xr-x 1 test_site test_site 152 Jun 18 08:27 about.html
drwxr-xr-x 2 test_site test_site 4096 Jun 18 08:27 css
-rwxr-xr-x 1 test_site test_site 152 Jun 18 08:27 index.html
I'm new to linux so please be gentle :)
I can confirm that test_site users is in the www-data group
id test_site
uid=1004(test_site) gid=1005(test_site) groups=1005(test_site),33(www-data)
I think what you are looking for is umask
umask 0022
umask -p
umask -S
You will need to modify your default /etc/profile in order to make this permanent. umask 0022will on creation give directories chmod 755 and files chmod 644 which is the recommended permissions for the www folder in apache.
Example
umask 0022
mkdir www
touch www/{index.html,style.css}
drwxr-xr-x. 2 user user 4096 Jun 18 10:53 .
drwxr-xr-x. 3 user user 4096 Jun 18 10:52 ..
-rw-r--r--. 1 user user 0 Jun 18 10:53 index.html
-rw-r--r--. 1 user user 0 Jun 18 10:53 style.css
FTP also supports setting umask before initiating a transfer. Setting it with ftp, sets if for that transfer only, so that you don't need to change it system-wide as you would with /etc/profile.

tar: can't change directories to <dir>/<file>: not a directory

I need to tar this dir - totar:
$ ls -l totar/
total 61388
292 -rw-r--r-- 1 wasext was 298867 Apr 13 16:44 application.2015-01-19.0.log
108 -rw-r--r-- 1 wasext was 109623 Apr 14 13:32 application.2015-01-20.0.log
788 -rw-r--r-- 1 wasext was 805468 Apr 14 13:32 application.2015-01-21.0.log
1080 -rw-r--r-- 1 wasext was 1104861 Apr 14 13:32 application.2015-01-22.0.log
2052 -rw-r--r-- 1 wasext was 2098782 Apr 14 13:32 application.2015-01-23.0.log
4 -rw-r--r-- 1 wasext was 455 Apr 14 13:32 application.2015-01-24.0.log
21076 -rw-r--r-- 1 wasext was 21581073 Apr 14 13:32 application.2015-01-26.0.log
9296 -rw-r--r-- 1 wasext was 9519026 Apr 14 13:32 application.2015-01-27.0.log
17912 -rw-r--r-- 1 wasext was 18341302 Apr 14 13:32 application.2015-01-28.0.log
8780 -rw-r--r-- 1 wasext was 8989019 Apr 14 13:32 application.2015-01-29.0.log
I created tarball file with parameter "-C":
$ tar cvf t.tar -C totar/*
tar: can't change directories to totar/application.2015-01-19.0.log: Not a directory
a totar/application.2015-01-20.0.log 215 blocks
a totar/application.2015-01-21.0.log 1574 blocks
a totar/application.2015-01-22.0.log 2158 blocks
a totar/application.2015-01-23.0.log 4100 blocks
a totar/application.2015-01-24.0.log 1 blocks
a totar/application.2015-01-26.0.log 42151 blocks
a totar/application.2015-01-27.0.log 18592 blocks
a totar/application.2015-01-28.0.log 35823 blocks
a totar/application.2015-01-29.0.log 17557 blocks
I got this error:
tar: can't change directories to totar/application.2015-01-19.0.log: Not a directory
In created tar file is missing file: totar/application.2015-01-19.0.log:
$ tar tf t.tar
totar/application.2015-01-20.0.log
totar/application.2015-01-21.0.log
totar/application.2015-01-22.0.log
totar/application.2015-01-23.0.log
totar/application.2015-01-24.0.log
totar/application.2015-01-26.0.log
totar/application.2015-01-27.0.log
totar/application.2015-01-28.0.log
totar/application.2015-01-29.0.log
Can you help me where I made mistake?
With the -C option to tar, you are telling tar to change to that directory first before adding files to the archive. This is very convenient to prevent having to change to that directory to begin with. Say you are in your $HOME directory and want to archive the files in /path/to/totar, then you simply give the -C option to the directory above totar and target directory. If you want to archive /path/to/totar, then you want to:
tar -C /path/to cvf somearchive.tar totar
Note the files spec above totar is a directory name not a filename and will include all files in the totar directory. You do not include wildcards, but you can include specific filenames. To backup only some files in /path/to/totar:
tar -C /path/to/totar cvf somearchive.tar filename
to only filename in the totar directory. (wildcards are not allowed)
I'll see if I can make it clearer: When you enter anything with *, your shell immediately expands it to a list of files that match the patten.
So say you have a simpler directory: totar contains just 3 files: foo, bar, and foobar
When you then type:
tar cvf t.tar -C totar/*
It gets expanded to
tar cvf t.tar -C totar/foo totar/bar totar/foobar
Since -C takes a parameter, tar interprets the next argument, totar/foo, as the parameter of -C. And only totar/bar and totar/foobar remain as the files to be archived.
Do you really need the -C? Because I suggest leaving the -C out completely
$ tar cvf t.tar totar/*
Update:
If you want to retain the behaviour of -C, I strongly suggest that you have a look at #charles-duffy 's comments below.
Cheers

Install rpm and put file into symbolic link directory

I have made an rpm package that install a program and one of the folders it needs to copy a file to is a symbolic link since the program the symbolic link is pointing to may change over time so it is easier to maintain the building of the rpm package by copying the file to the symbolic link rather then to the hard coded path. However, I get the error
cp: cannot overwrite directory with non-directory
when the rpm package tried to copy the file to the symbolic link folder. Why does this happen, and is there anything I can do to work around this error other then making the files to be copied to the folder the symbolic link points to? I am running RHEL 6.6 as of note.
That error generally means something like you having told cp to treat the target as a normal file (the -T argument).
$ ls -lR
.:
total 16
drwxr-xr-x 2 root root 4096 Feb 6 09:46 dir
-rw-r--r-- 1 root root 0 Feb 6 09:45 file
lrwxrwxrwx 1 root root 3 Feb 6 09:45 symdir -> dir
./dir:
total 0
$ cp -T file symdir
cp: cannot overwrite non-directory `symdir' with non-directory
$ ls -lR
.:
total 16
drwxr-xr-x 2 root root 4096 Feb 6 09:46 dir
-rw-r--r-- 1 root root 0 Feb 6 09:45 file
lrwxrwxrwx 1 root root 3 Feb 6 09:45 symdir -> dir
./dir:
total 0
$ cp file symdir
$ ls -lR
.:
total 16
drwxr-xr-x 2 root root 4096 Feb 6 09:46 dir
-rw-r--r-- 1 root root 0 Feb 6 09:45 file
lrwxrwxrwx 1 root root 3 Feb 6 09:45 symdir -> dir
./dir:
total 4
-rw-r--r-- 1 root root 0 Feb 6 09:46 file

scp file not setting correct owner

Does SCP have a problem setting file permissions or have I misconfiguration my server?
Use case:
There is a file on a server that I want to edit called "importantFile.txt". The file has owner and group of "master":
ls -l importantFile.txt:
-rw-rw-r-- 1 master master 7 Mar 18 08:11 importantFile.txt
I am called "slave" but luckily, I am in group "master" so I can edit the file as I see fit. However, I'm a lazy slave and can't be bothered to edit the file on the server, I'd prefer to edit the file on my local machine and SCP it to the server:
echo "bored slave info" > importantFile.txt
scp importantFile.txt slave#theServerAddress:/pathToFile/importantFile.txt
If I do this, the contents of the file on the server are uploaded fine and the timestamp of the file is updated but the permissions of the file don't change, the file is still owned by "master". This is a problem because if "slave" uploaded bad content, no one would know it was "slave" who caused the problem, "master" would look guilty.
Perhaps I have to set a umask? if so where? I tried .bash_profile without success and haven't found anything on Google about umask in /etc/ssh/sshd_config.
That's nothing special about scp - try logging on to the server as slave, and editing the file using your favourite text editor... You'll find the same behaviour occurs... Writing to a file does not make you the owner of the file.
Example:
as root
#cd /tmp
#mkdir fubar
#chgrp vboxusers fubar
#cd fubar/
#touch testfile
#chgrp vboxusers testfile
#chmod g+w . testfile
#ls -al
total 16
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 0 2009-03-19 10:30 testfile
#echo foo > testfile
#ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 4 2009-03-19 10:30 testfile
as user (in vboxusers group)
>cd /tmp/fubar
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 4 2009-03-19 10:30 testfile
>echo bar >> testfile
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 8 2009-03-19 10:31 testfile
>vim testfile
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:31 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:31 ..
-rw-rw-r-- 1 root vboxusers 12 2009-03-19 10:31 testfile
>cat testfile
foo
bar
baz
You have to delete the file to overwrite it. Wether you are able to do that depends on the directory's permissions and ownership. Hijacking ownership of an already existing file is not possible. The write permission you have is only applied on the contents of the file.
It seems you can configure how Emacs deals with this through the backup-by-copying-when-mismatch variable (see the Emacs Manual or type C-h-v backup-by-copying-when-mismatch in Emacs).
I actually filed a bug report about this, because I thought it was a bug in Tramp.
I had misunderstood the way files work, modifying file contents do not change ownership or group.
Why the confusion? EMACS - Whenever I was editing a file I was using Emacs and Emacs does change the owner and group to the current user. It does this because it makes a backup file at save time by moving the "filename" to "filename~" and creating a new file called "filename" - because it's a new file, it has the current users file permissions. I guess this is 1up to VI fans?

Resources