How to create an archive and chmod it - linux

I should create a file inside this file an archive. When I create it, I
should use chmod so that the archive should have 757 rights.
I did this but I failed, is this right or wrong? :
$ mkdir file1
$ cd file1
# here i should create an archive but how i dont know
$ chmod 757 archivename

Use touch command first:
bash-4.3$ mkdir a
bash-4.3$ cd a
bash-4.3$ pwd
/home/cg/root/a
bash-4.3$ touch a.txt
bash-4.3$ chmod 757 a.txt
bash-4.3$ ls -lrt
total 0
-rwxr-xrwx 1 18207 18207 0 May 4 11:29 a.txt

I think this is what you need:
$ mkdir file1
$ cd file1
$ touch actual_file.txt
$ cd ..
$ tar czvf file1-archive.tar.gz file1/
$ chmod 757 file1-archive.tar.gz
This creates an archive of everything inside the file1 folder.

Related

I have problems with permissions using xargs with rm

I have remove.txt file containing a.txt and b.txt as text. All files are in the same folder, I set chmod 777 for all those files. Now, when I run sudo cat remove.txt | xargs rm I get Permission denied for deletion of those two files listed in remove.txt. What am I doing wrong? I guess question now would be where to put sudo?
sudo cat remove.txt | xargs sudo rm

for loop on ssh bash to rename multiple files

Have created set of files
test1, test2, test3
the above files are on different server name xyz.com
If at all i need to rename the files with prefix
ex: test1 > old_test1
test2 > old_test2
test3 > old_test3
I tried:
#!/bin/bash
ssh $xyz.com -t -t /bin/bash <<EOF
cd /tmp
for i in {test*}
do
mv -v {i} "{i/test/old_test}"
ls -ltr
done
exit
but the output is just mv -v followed by list of files and nothing is happening (rename).
!/bin/bash
ssh $xyz.com 'bash' <<'ENDSSH'
cd /tmp
for i in {test*}
do
mv "${i}" "${i/test/old_test}";
ls -ltr
done
exit
change your rename-command to the following:
mv -v "$i" "${i/test/old_test}";
I tried your problems as follow.
In my directory are the following files:
test1
test2
test3
With this command:
for i in $(ls); do (mv "$i" "${i/test/old_test}"); done
i rename all files.
My files are now:
old_test1
old_test2
old_test3
Please change your for-loop like the this:
#!/bin/bash
ssh $xyz.com -t -t /bin/bash <<EOF
cd /tmp
for i in $(ls);
do
mv -v "$i" "${i/test/old_test}";
ls -ltr
done

Move multiple files with unique name to new folder and append to file name

I have about 2000 files in a folder.
All the files contain the string test in the name.
What I need to do is move all those files ~1250 to a folder called trash within the same directory and append _scrap to the end of each file.
mv *test* trash/
What I want is something like this:
[root#server] ls
test1.txt test2.txt test3.txt trash video1.txt video2.txt video3.txt
[root#server] mv *test* trash/*_scrap
[root#server] ls
trash vidoe1.txt video2.txt video3.txt
[root#server] ls trash/
test1.txt_scrap test2.txt_scrap test3.txt_scrap
I can move all files, however I cannot figure out how to append the _scrap to the end.
As I have to do this on a number of machines, a one liner would be preferable over a small script.
$ touch test1.txt test2.txt test3.txt vidoe1.txt vidoe2.txt vidoe3.txt
$ mkdir trash
$ for file in *test*; do mv "$file" "trash/${file}_scrap"; done
$ ls
trash vidoe1.txt vidoe2.txt vidoe3.txt
$ ls trash
test1.txt_scrap test2.txt_scrap test3.txt_scrap
$
You could also use xargs
$ ls *test* | xargs -t -I{} mv {} trash/{}_scrap
mv test1.txt trash/test1.txt_scrap
mv test2.txt trash/test2.txt_scrap
mv test3.txt trash/test3.txt_scrap
$
You could use find
$ find . -name '*test*' -maxdepth 1 -exec mv {} trash/{}_scrap \;
You can use rename to avoid shell for loops. It's a perl script but it comes installed with many common distros (including Ubuntu 14):
$ mv *test* trash/
$ rename 's/$/_scrap/g' trash/*
$ ls trash/
test1.txt_scrap test3.txt_scrap test2.txt_scrap

Does $PWD always equal $(realpath .)

Given
A modern Linux/UNIX/OSX (w/ realpath)
bash 4+ (even on OSX)
Is
"$PWD" == "$(realpath .)"
Always true?
It's pretty easy to test that this is not always the case.
$ mkdir /tmp/realdir
$ cd /tmp/realdir
$ echo $PWD
/tmp/realdir
$ ln -s realdir /tmp/fakedir
$ cd /tmp/fakedir
$ echo $PWD
/tmp/fakedir
$ realpath .
/tmp/realdir
so no, $PWD is not always the same as $(realpath .).
The bash manual indicates that the PWD variable is set by the built-in cd command. the default behaviour of cd is:
symbolic links are followed by default or with the -L option
This means that if you cd into a symlink the variable gets resolved relative to the symlink, not relative to the physical path. You can change this behavior for a cd command by using the -P option. This will cause it to report the physical directory in the PWD variable:
$ cd -P /tmp/fakedir
$ echo $PWD
/tmp/realdir
You can change the default behavior of bash using the -P option:
$ set -P
$ cd /tmp/fakedir
$ echo $PWD
/tmp/realdir
$ set +P
$ cd /tmp/fakedir
$ echo $PWD
/tmp/fakedir
This is of course notwithstanding the fact that you can assign anything you want to the PWD variable after performing a cd and it takes that value:
$ cd /tmp/fakedir
$ PWD=/i/love/cake
$ echo $PWD
/i/love/cake
but that's not really what you were asking.
It is not necessarily the case even when symbolic links are not used and PWD is not set by the user:
vinc17#xvii:~$ mkdir my_dir
vinc17#xvii:~$ cd my_dir
vinc17#xvii:~/my_dir$ rmdir ../my_dir
vinc17#xvii:~/my_dir$ echo $PWD
/home/vinc17/my_dir
vinc17#xvii:~/my_dir$ realpath .
.: No such file or directory
Note that under zsh, ${${:-.}:A} still gives the same answer as $PWD (the zshexpn(1) man page says about the A modifier: "Note that the transformation takes place even if the file or any intervening directories do not exist.").
Note that however, $PWD contains obsolete information. Using it may be a bad idea if some other process can remove the directory. Consider the following script:
rm -rf my_dir
mkdir my_dir
cd my_dir
echo 1 > file
cat $PWD/file
rm -r ../my_dir
mkdir ../my_dir
echo 2 > ../my_dir/file
cat ./file
cat $PWD/file
rm -r ../my_dir
It will output:
1
cat: ./file: No such file or directory
2
i.e. $PWD/file has changed.

How I can pack into tar-file only last directory from path?

How I can pack into tar-file only last directory from path?
For example, I have several paths
/usr/local/files/dir1/
file1.txt
file2.txt
file3.txt
/usr/local/files/dir2/
file3.txt
file4.txt
file5.txt
if I run command:
tar czf my_arch.tar.gz -C /usr/local/files/dir1 .
I gain only containment of dir1 catalog, without itself.
So I have - my_arch.tar.gz/file1.txt, file2.txt, file3.txt, But I need structure like that inside my archive -
my_arch.tar.gz/dir1/file1.txt, file2.txt, file3.txt
How I can do this?
Thank you.
try
cd /usr/local/files
tar -cvzf my_arch.tar.gz dir1
The -C directive will make you change into dir1 and thus not archive the folder, but its contents:
-C, --directory DIR
change to directory DIR
you cannot do this directly through tar.
here's my suggestion :
#!/bin/bash
mydir=/my_dir/whit/long_and/complicated_path/the_stuff_is_here
dirname=$(dirname $mydir )
basename=$(basename $mydir )
tar cvf /tmp/$basename.tar -C $dirname $basename
$ tar vczf tmp/export/files.tar.gz -C tmp/export src
structure for files.tar.gz
src
src/app
src/app/main.js
src/app/util
src/app/util/runtime.js
If I understand what you are asking correctly, you want your tar file to contain the directory.
Try it without the -C flag as in:
tar -czf my_arch.tar.gz /usr/local/files/dir1
If you specify -C then you directory path is ./. Probably the following works like you want:
$ touch asdf/foo/bar/{1,2,3}
$ tree asdf/
asdf/
└── foo
└── bar
├── 1
├── 2
└── 3
2 directories, 3 files
$ tar -cv -C asdf/foo/bar/ -f asdf.tar ./
./
./3
./2
./1
$ tar tf asdf.tar
./
./3
./2
./1

Resources