Create a tar file in a subdirectory of the directory to be archived - unix

I'd like to create a tar file of all the files in a directory minus sub-directory's in that directory and place that tar file in one of the sub-directory's. For example, I have several .txt files in /test and also another directory in /test called ArchivedFiles. I'd like to tell the tar command to archive all of the .txt files and place it in /test/ArchivedFiles.
How do I go about doing this?

tar cf test/foo/test.tar -- `find test -maxdepth 1 -name '*.txt' -type f`
I think that should do what you want.
An option which will not work due to the age of your tar command is:
find test -maxdepth 1 -type f -name '*.txt' -print0 | tar -cf test/foo/test.tar --null --files-from -
You are having problems, so you can try the following commands:
tar cf test/foo/test.tar `find test -maxdepth 1 -name '*.txt' -type f`
echo tar cf test/foo/test.tar `find test -maxdepth 1 -name '*.txt' -type f`
tar c f test/foo/test.tar `find test -maxdepth 1 -name '*.txt' -type f`
find test -maxdepth 1 -name '*.txt' -type f
And pastebin the output so that we can see what is happening.
Given that find is very legacy as well, let us try the following:
tar cf test/foo/test.tar test/*.txt

The following command will work. It will place any subdirectories into the tar but it doesn't put the contents of those subdirectories into the tar. This means that if you put the tar into a subdir you won't have to worry about it putting itself inside itself inside itself...
For example, if you want to put all of the files which are in the current directory into a tar file called mytar.tar which is in a subdir called d1:
tar --no-recursion -cvf d1/mytar.tar *

Related

How do you move all files and folders within a directory to the parent directory?

How do you move all files and folders within a directory from their sub directories to the parent directory? Including files within very deep folder directories.
What I would like to achieve is for when I am at . to convert this:
.
./aDir
./aDir/bFile
./aDir/cDir
./aDir/cDir/dDir
./aDir/cDir/dDir/eFile
To this:
.
./aDir
./bFile
./cDir
./dDir
./eFile
I assume you use the unix command find however I can't seem to get it to work.
Here's what I tried:
find -mindepth 1 -maxdepth * -print0 | xargs -0 mv -i -t ~/Desktop
Since you are looking to un-nest your directory you'll need to mv them in depth first order, otherwise deeper directories could be mv'd inside the shallower ones.
Using very similar syntax to your attempt the following seems to do what is required.
find . -mindepth 2 -depth -type d -print0 | xargs -0 -I{} mv {} ~/Desktop
Example:
$ find . -mindepth 1 -depth -type d
./a/b/c2
./a/b/c
./a/b
./a
$ find . -mindepth 2 -depth -type d -print0 | xargs -0 -I{} mv {} .
$ find . -mindepth 1 -depth -type d
./a
./b
./c
./c2
Maybe this helps:
for i in $(find .); do cp -r $i .; done
When you are at . it converts this:
.
./a
./a/b
./a/b/c
To this:
.
./a
./a/b
./a/b/c
./c
./b
./b/c
Try this
find . -maxdepth 1 -exec mv {} .. \;
you might get this message
mv: cannot move `.' to `../.': Device or resource busy
But don't worry it is because '.' this directory is being attempted to move.
I think you want:
find . -mindepth 1 -depth -print0 | xargs -0 mv -i -t ~/Desktop

Unix: find files in every subdirectory named upd

I have directory tree structure which looks like this
/app/bad/upd /app/pass/upd /app/bad/upd /app/warn/upd
I want to build a find command which can list all the files in every sub-directory named upd.
Currently I list it individually
find /app/bad/upd -type f -name "*${FILE_NAME}*"
This might be what you look for:
find /app -type d -name upd -exec ls -l {} +
or perhaps:
find /tmp/* -type d -name upd -exec sh -c "ls -l {}/*${FILE_NAME}* 2>/dev/null" sh {} \;
If the upd directory is always in the 2nd directory down, you could simply do:
ls /app/*/upd

Move only directories and not files in Unix OS

I had a directory with many files and sub-directories. To move only the sub-directories, I just learned you can use:
ls -d BASEDIR/*/ | xargs -n1 -I% mv % TARGETDIR/
I use the following:
$ mv ./*/ DirToMoveTo
For example:
Say I wanted to move all directories with "Old" in the name to a folder called "Old_Dirs" on /data.
The command would look like this:
mv ./*Old*/ /data/
Why not use find?
find . -maxdepth 1 -type d -exec mv '{}' /tmp \;
-maxdepth 1 makes sure find won't go deeper than current directory
-type d tells find to only find directories
-exec execute a command with the result of the find referenced by {}
In my opinion a cleaner solution and it also works better then using xargs when you have files with white space or tabs in them.
With a file structure like this:
/dir2move2
/dir
/subdir1
/subdir2
index.js
To move only the sub directories and not the files you could just do:
mv ./dir/*/ ./dir2move2
Possible solution:
find BASEDIR/ -maxdepth 1 -mindepth 1 -type d -exec mv '{}' TARGETDIR \;
you can simply use the same command for moving a file but put a slash after the name of the subdirectory
sudo mv */ path/to/destination
sudo mv subdir/ path/to/subdirectory

Find and Tar multiple files while keeping original names

I want to find several directories, and make each one a Tar. My current find command sends the filenames to a file for logging.
find ${SRC_DIR} -name ./* -level 0 -type d -mtime +14 -exec basename {} \; >>${FILE}
This works fine. Now I want to take each of the files that I found and Tar them all, so that they're named OriginalFileName.tar.
Is there a way to do this all in one command, and how do I get the Tar files to have the original filenames?
Does this help your problem:
for i in $(find ${SRC_DIR} -name ./* -level 0 -type d -mtime +14 -exec basename {} \;); do tar -cvf $i.tar $i; done

Tar only the Directory structure

I want to copy my directory structure excluding the files. Is there any option in the tar to ignore all files and copy only the Directories recursively.
You can use find to get the directories and then tar them:
find .. -type d -print0 | xargs -0 tar cf dirstructure.tar --no-recursion
If you have more than about 10000 directories use the following to work around xargs limits:
find . -type d -print0 | tar cf dirstructure.tar --no-recursion --null --files-from -
Directory names that contain spaces or other special characters may require extra attention. For example:
$ mkdir -p "backup/My Documents/stuff"
$ find backup/ -type d | xargs tar cf directory-structure.tar --no-recursion
tar: backup/My: Cannot stat: No such file or directory
tar: Documents: Cannot stat: No such file or directory
tar: backup/My: Cannot stat: No such file or directory
tar: Documents/stuff: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
Here are some variations to handle these cases of "unusual" directory names:
$ find backup/ -type d -print0 | xargs -0 tar cf directory-structure.tar --no-recursion
Using -print0 with find will emit filenames as null-terminated strings; with -0 xargs will interpret arguments that same way. Using null as a terminator helps ensure that even filenames with spaces and newlines will be interpreted correctly.
It's also possible to pipe results straight from find to tar:
$ find backup/ -type d | tar cf directory-structure.tar -T - --no-recursion
Invoking tar with -T - (or --files-from -) will cause it to read filenames from stdin, expecting each filename to be separated by a line break.
For maximum effect this can be combined with options for null-terminated strings:
$ find . -type d -print0 | tar cf directory-structure.tar --null --files-from - --no-recursion
Of these I consider this last version to be the most robust, because it supports both unusual filenames and (unlike xargs) is not inherently limited by system command-line sizes. (see xargs --show-limits)
for i in `find . -type d`; do mkdir -p /tmp/tar_root/`echo $i|sed 's/\.\///'`; done
pushd /tmp/tar_root
tar cf tarfile.tar *
popd
# rm -fr /tmp/tar_root
go into the folder you want to start at (that's why we use find dot)
save tar file somewhere else. I think I got an error leaving it right there.
tar with r not c. I think with cf you keep creating new files and you only
get the last set of file subdirectories. tar r appends to the tar file.
--no-recursion because the find is giving you your whole list of files already
so you don't want to recurse.
find . -type d |xargs tar rf /somewhereelse/whatever-dirsonly.tar --no-recursion
tar tvf /somewhereelse/whatever-dirsonly.tar |more to check what you got.
For AIX:
tar cvfD some-tarball.tar `find /dir_to_start_from -type d -print`

Resources