"tar --directory" won't work [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I want to create a tarball of files, but not include the directory name. I know there's another way to do it, but I want to know why this way is not working.
If I run the following to create the tarball comprised of a specific file, then it works:
tar -vcf file.tar -C <PATH TO DIR> file1
However, if I run it on a wild card to include multiple files, then it fails:
tar -vcf file.tar -C <PATH TO DIR> *
I get an error saying, for each file in the current dir (not the dir specified in -C), tar: <FILE>: Cannot stat: No such file or directory
Any idea why running the above command on a wild card vs a file name behaves differently?

The * wildcard is expanded by the shell before tar is invoked. tar then changes directory (because you asked it to), but then can't find the files that were in the shell's current directory.
Of course, changing directories in the shell means that you can't open the output file in the original current directory. So you have to redirect the output of tar outside of the subshell, like this:
(cd $DIR; tar -vc *) > file.tar

You have to use a directory:
tar -vcf files.tar /path/to/directory
If your files are in your folder, then use the . to reference the current folder:
tar -vcf files.tar .

Related

Cygwin move file from folder to folder on Desktop and open Dreamweaver [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I use Cygwin, and I write commands. On my Desktop I have two folders, named folder1 and folder2. In folder1 there is a text file. I want to move the text file to folder2. But when I use the mv command it doesn't work. I get an error message "no such file or directory".
My question is: how can I move file.txt from folder1 to folder2?
My code is:
/cygdrive/c/Users/Maichel/Desktop/folder1
$ mv file.txt folder2/.
This also doesn't work:
/cygdrive/c/Users/Maichel/Desktop/folder1
$ mv Desktop/file.txt Desktop/folder2/.
I have one question more: How can I open Dreamweaver in Cygwin? If I use notepad file.txt then the file is opened in notepad. How do I do this with Dreamweaver?
You need to reference your paths properly. If your directory hierarchy looks like this:
Desktop/
- folder1/
- folder2/
Then from inside folder1, you need to navigate up one directory with .. to get to Desktop, then go back down into folder2:
/cygdrive/c/Users/Maichel/Desktop/folder1
$ mv file.txt ../folder2/
You can also use the absolute path:
/cygdrive/c/Users/Maichel/Desktop/folder1
$ mv file.txt /cygdrive/c/Users/Maichel/Desktop/folder2/

linux bash - remove all files which are in one directory from another directory [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I want to remove all files that exist in folder new-files from another folder in linux using bash commands.
I need this for two things:
I got some setup scripts which copy some pre-configured config files over. I would like to have the option to remove those files again
Sometimes it happens that archives get unpacked into the root of your downloads directory and not into a subdir because the person packing the file put everything to the archives root
What's the best way to do that?
Edit, to clarify:
I got a folder with files called new-files.
Now I execute cp -r new-files/* other-directory/.
Lets say other-directory is not the directory I wanted to copy them to but it already contains other files so I can't just do rm other-directory/*.
I need to delete all folders which I accidently copied. How do I do that?
You could use the following command:
cd new-files ; find . -exec rm -rf path/to/other-directory/{} \;
It will list all the files that where copied from the new-files directory (new-files directory will not be taken in consideration). For each file, it will remove the copied version in other-directory.
But you've to be careful, if a file in new-files erase a file in other-directory, you won't be able to restore the old file using this method. You should consider to use a versioning system (like Git for example).
From your:
Edit, to clarify:
I got a folder with files called new-files.
Now I execute cp -r new-files/* other-directory/.
Lets say other-directory is not the directory I wanted to copy them to but it already contains other files so I can't just do rm
other-directory/*.
I need to delete all folders which I accidently copied. How do I do that?
You can loop through the original dir new-files/ and delete files with same name in the other-directory/:
for file in /new-files/*
do
rm /other-directory/"$file"
done
wee script to do what you want:
pushd `pwd`
cd /path/to/new-files
x=`find . -type f`
popd
echo $x | xargs rm

Unzip and move a downloaded file - Linux [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Surprisingly I could not find a straight-forward answer to this question on here yet. I am still learning Linux. Say I have downloaded a zip file to my Downloads folder. Now, I want to move it into a protected folder, like /opts or /var. Is there a good command to both sudo move AND unzip the file to where I need it to go?
If you wish to perform two separate operations (move and extract) then you have no option but to use two commands.
However, if your end goal is to extract the zip file to a specific directory, you can leave the zip file where it is and specify an extraction directory using the -d option:
sudo unzip thefile.zip -d /opt/target_dir
From the manpage:
[-d exdir]
An optional directory to which to extract files. By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory). This option need not appear at the end of the command line; it is also accepted before the zipfile specification (with the normal options), immediately after the zipfile specification, or between the file(s) and the -x option. The option and directory may be concatenated without any white space between them, but note that this may cause normal shell behavior to be suppressed. In particular, ''-d ~'' (tilde) is expanded by Unix C shells into the name of the user's home directory, but ''-d~'' is treated as a literal subdirectory ''~'' of the current directory.
sudo mv <file_name> /opts && unzip /opts/<file_name>
Also you may specify the unzip destination to unzip so you can do this in a single command. This however will be a bit different from the command above as the zip will be kept in its current location, only the unzipped files will be extracted to the pointed destination.
unzip -d [target directory] [filename].zip

How to make a tar backup of a root filesystem? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have linux installed on SD card, I used this command to install the rootfs
tar xpjf rootfs.tar.bz -C /mnt/rootfs/
Now, I made some changes to the rootfs and I would like to create a backup that I can use with the same command above, I tried using:
tar cpjf rootfs.tar.bz2 /mnt/rootfs
and
tar cpjf rootfs.tar.bz2 -C / mnt/rootfs
I also tried
tar cpjf rootfs.tar.bz2 /mnt/rootfs/*
And tried:
cd /mnt/rootfs
tar -cvpjf rootfs.tar.bz2 --exclude=/rootfs.tar.bz2 .
tar: ./rootfs.tar.bz2: file changed as we read it
but I end up with an archive that has two levels before the file system i.e mnt/rootfs/files What am I doing wrong ?
That's because it starts from current working directory, you can do:
cd /mnt/rootfs
tar cpjf /rootfs.tar.bz2 .
And that should create an archive at /rootfs.tar.bz2 with its root at the contents of /mnt/rootfs/

How to strip path while archiving with TAR [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I have a file that contain list of files I want to archive with tar.
Let's call it mylist.txt
It contains:
/path1/path2/file1.txt
/path1/path2/file3.txt
...
/path1/path2/file10.txt
What I want to do is to archive this file into a tarball but excluding /path1/path2/.
Currently by doing this:
tar -cvf allfiles.tar -T mylist.txt
preserves the path after unarchiving.
I tried this but won't work too:
tar -cvf -C /path1/path2 allfiles.tar -T mylist.txt
It archives all the files in /path1/path2 even those which are not in mylist.txt
Is there a way to do it?
In your "Extraction phase" you can use the strip-components flag like
tar xvf tarname.tar --strip-components=n
which will remove the first n leading components of the file name. Although if you have different file-path-components this will not work for all cases.
If you want to do it while archiving, only one thing comes to mind, and I will share
INPUT: list of files + full paths
1) for each line, split the path out of the filename
2) execute cd to that path and tar on that filename
3) repeat for each line

Resources