Remove a file has the same name with folder after using tar - linux

I created a script to find files, move them to a folder, compress this folder using tar. Then delete original folder. But after running the script, the folder was removed but a file having same name with the folder was created. I tried to run one by one command, it's OK. There is not this file. I added rm command in the script to remove it but not working.
I don't know why this file was create.
My script is below:
#!/bin/bash
cd /home/tuan/testrm/9/
mkdir compressdir
sudo find . -type f -name "*.txt" -print | xargs -I {} mv {} compressdir > /dev/null 2>&1 &
tar -cvzf compresslog.tar.gz --remove-files compressdir
mv compresslog.tar.gz compresslog-`date +"%d%m%Y"`.tar.gz
rm -rf compressdir
I want to know why this file was create and how to prevent this happen.

You should remove & at the end of sudo find line, and it will works.
Because of The & makes the command run in the background.
Root cause: the sudo find command line and tar -> mv -> rm run at synchronized.
If you had checked the file compresslog.tar.gz which the script generated, you will found that it was null or error, and the compress file contain the same content with someone file which you find.

Related

Merge and compile SCSS files recursively using shell script in Windows

So here's the situation. I currently have project with the following folder structure:
-- public_html
-- assets
-- scss (contains SCSS files, which can be located inside subfolders)
-- scripts
-- vendor
-- plugins (Also contain some SCSS files from the)
-- css
-- also some other folders (no scss or css here though)
-- dist (distribution folder)
-- other folders too for html, php etc...
When I want to upload the script to my remote server, I firstly process the complete folder structure in public_html to the dist. I do this with a build script, build.sh, which you can see here:
function prepareForBuild(){
echo "Updating dependencies...";
bower install
echo "Preparing 'dist' directory...";
mkdir -p dist
rm -rf dist/*
# for the stackoverflow markup... */
}
function buildApp(){
cd assets
echo "Creating temp directory..."
mkDir -p temp
## MERGING TAKES (OR SHOULD) PLACE HERE
find "/" -name '*.scss' -exec cat {} \; > merge.scss
cd ../
pause
# requirejs optimization
echo "Optimizing files...";
node r.js -o build/build.js
}
function cleanup(){
echo "Cleaning up unnecessary files...";
cd dist
rm -f bower.json .gitignore .bowerrc README.md .DS_Store config.rb
rm -f build.sh build.txt composer.json composer.lock
rm -rf build dist scss .git .sass-cache .idea
}
function pause(){
read -p "$*"
}
prepareForBuild
buildApp && cleanup
echo "Building finished!";
Problem:
However, when I run the script in the Git bash terminal it breaks at this line:
find "/" -name '*.scss' -exec cat {} \; > merge.scss
Edit: "/" or "\" does not seem to affect the problem :(
Also, run from root (public_html) directory:
where it returns in the normal command prompt "File not found - *.scss" (and outputs an empty merge.scss file) and simply stops working in the Git bash terminal.
I based this line on this answer, but I can't get it to work for me. I also tried something like this instead but this only returns more errors.
What am I missing here?
EDIT!
I thought the bash shell simply stopped working but it seems that was not the case. After a while it returns this:
What does this mean? Is the reference folder incorrect?
Objective:
As said I would like to recursively scan the assets folder for .scss files and merge them into one default.scss.
If this is too much to ask another solution integrated with require.js (or simply with node) would also be possible, as you can see in my script I already use require.js for standard optimization.
Thanks for your time and patience!
If you want to scan for files under the assets directory, then do not specify the root directory in the find command. Omitting directories might also be a good idea.
find . -name "*.scss" -not -type d -exec cat {} \; > merge.scss
In the chance the you are wanting the merge.scss file to be created in the newly created temp directory, you need to make it the current directory. Then you can do the file scan from the parent, assets, directory.
function buildApp(){
cd assets
echo "Creating temp directory..."
mkDir -p temp
## MERGING TAKES (OR SHOULD) PLACE HERE
cd temp
find .. -name '*.scss' -not -type d -exec cat {} \; > merge.scss
cd ../
pause
# requirejs optimization
echo "Optimizing files...";
node r.js -o build/build.js
}
Also, does a pause command work for you in the bash shell? That is a Windows command unless you have something else.
Your -exec option misses the {} special element, meaning the file matching each find iteration. In addition, you can't definitively use '\' as a root path, at least it would be '/'.
Nevertheless, ideally you should use a specific sub-path for better efficiencies, and for permissions access reasons (I guess your use may not have permission to access all sub path from the root path?).
At least, you can update your instruction to:
find '/' -type f -name "*.scss" -exec cat {} \; > merge.scss

delete all folders and files within a linux directory except one folder and all contents inside that folder

I have a directory structure as :-
/usr/testing/member/
---> public--->folder1--->file1
\----> file2
---> folder3:- contains files and folders
---> folder4:- contains files and folders
---> several files
I want to keep the public folder and all its contents (further folders and files within it) but want to delete everything else under the directory /usr/testing/member/. But that also means member folder is not deleted.
Is there any shell script or command that can be used to achieve this exactly as i stated.
Here's one way to do it:
(cd /usr/testing/member; find . -maxdepth 1 \( ! -name . -a ! -name public \) -exec echo rm -fr {} +)
That is: cd into /usr/testing/member, find all files and directories there, without going further below, and exclude the current directory (".") and any file or directory named "public", and execute a command for the found files.
This will print what would be deleted.
Verify it looks good, and then drop the echo.
I think below will do the work,
$ cd /usr/testing/member/
$ rm -rf $(ls | grep -v "public")
explanation:
we are passing everything inside /usr/testing/member/ but public to rm by making use of -v(exclude) option of grep

Download, extract and copy file from a folder that has a version in its name

I'm writing a bash script that downloads an compressed archive from an universal URL (in which new release of the software will automatically be presented), extracts it and copies a file called wimboot to a folder.
This is what I currently have:
sudo curl http://git.ipxe.org/releases/wimboot/wimboot-latest.tar.gz -o ./wimboot.tar.gz
sudo tar -zxvf ./wimboot.tar.gz #Extracts into a folder called "wimboot-2.5.2-signed", in it is the file I need ("wimboot").
cd ./wimboot*/
sudo cp wimboot /my-folder/
But this doesn't work. Is there a method that will allow me to do this?
You can ask tar for a file listing (-t option), which you can then grep for wimboot -- that should give you the relative path to the file also. A naive first try would be something like:
src_file=$(tar tf wimboot.tar.gz | grep wimboot)
cp "$src_file" my_folder/
But you will probably want to add some error checking and stuff to that. And probably a more complicated grep expression to ensure you get the one thing you're after.
There's also no need to extract the entire archive. You can just ask tar to extract the file you're interested in:
tar zxf wimboot.tar.gz "$src_file"
I've built on top of the other answers and this worked for me:
#Create a temporary folder, this folder must be empty!
sudo mkdir temp
#Download the archive and save it in the temporary folder.
sudo curl http://git.ipxe.org/releases/wimboot/wimboot-latest.tar.gz -o ./temp/wimboot-latest.tar.gz
#Extract the downloaded archive to the temporary folder.
sudo tar xvf ./temp/wimboot-latest.tar.gz -C ./temp
#Search for and copy files with the name "wimboot" to the web directory.
sudo find ./temp/ -name 'wimboot' -exec cp {} /var/www/ \;
#Delete the temporary folder.
sudo rm -Rf temp
I do not recommend this for large archives.

What is the linux command to move the files of subdirecties into one level up respectively

The path structure of the files on my server is similar to shown below,
/home/sun/sdir1/mp4/file.mp4
/home/sun/collection/sdir2/mp4/file.mp4
I would like to move the files of "mp4" into one level up(into sdir1 and sdir2 respectively)
So the output should be,
/home/sun/sdir1/file.mp4
/home/sun/collection/sdir2/file.mp4
I have no idea to do this, so not tried yet anything...
There are different ways to solve your problem
If you just want to move those specific files, run these commands:
cd /home/sun/
mv sdir1/mp4/file.mp4 sdir1/
mv sdir2/mp4/file.mp4 sdir2/
If you want to move all mp4 files on those directories (sdir1 and sdir2), run these commands:
cd /home/sun/
mv sdir1/mp4/*.mp4 sdir1/
mv sdir2/mp4/*.mp4 sdir2/
Edit:
Make a script that iterates all the directories:
Create a script and name it and edit it with your favorite editor (nano, vim, gedit, ...):
gedit folderIterator.sh
The script file content is:
#/bin/bash
# Go to the desired directory
cd /home/sun/
# Do an action over all the subdirectories in the folder
for dir in /home/sun/*/
do
dir=${dir%*/}
mv "$dir"/mp4/*.mp4 "$dir"/
# If you want to remove the subdirectory after moving the files, uncomment the following line
# rm -rf "$dir"
done
Save the file and give it execute permissions:
chmod +x folderIterator.sh
And execute it:
./folderIterator.sh
You can do this:
# move all .mp4 files from sdir1/mp4 to sdir1 directory
user#host:~/home/sun$ mv sdir1/mp4/*.mp4 sdir/
# move all .mp4 files from collection/sdir2/mp4 to collection/sdir2 directory
user#host:~/home/sun$ mv collection/sdir2/mp4/*.mp4 collection/sdir2/
# move only 1 file
user#host:~/home/sun$ mv sdir1/mp4/file.mp4 sdir/
user#host:~/home/sun$ mv collection/sdir2/mp4/file.mp4 collection/sdir2/
I suggest you use find and something like
cd /home/sun/sdir1/mp4/
find . -name "*" -exec mv {} /home/sun/sdir1/ \;
cd /home/sun/collection/sdir2/mp4/
find . -name "*" -exec mv {} /home/sun/collection/sdir2/ \;
Alternatively, you could use tar and something like
cd /home/sun/sdir1/mp4/
tar cfp - * | (cd ../ ; tar xvvf -)
# Make sure everything looks good
rm -rf mp4
cd /home/sun/collection/sdir2/mp4/
tar cfp - * | (cd ../ ; tar xvvf -)
# Make sure everything looks good
rm -rf mp4
The command to move a file (or directory) up one level is:
mv /home/sun/sdir1/mp4/file.mp4 ..
Wildcards can be used to select more files & directories, you can also provide more than one directory at a time.
mv /home/sun/sdir1/mp4/*.mp4 /home/sun/collection/sdir2/mp4/*.mp4 ..

linux shell tar unwanted extra directories

I have the following problem:
I have directorties a/b/c and inside c many text files.
I want to make a .tar.gz file in drectory a/mydir with the c directory inside and then unzip it to that same directory to create a/mydir/c (with all the files inside)
I am at directory a and run: (shell)
~:$ tar -czf mydir/output.tar.gz b/c
~:$ tar -zxf mydir/output.tar.gz -c mydir
but the result is directories a/mydir/b/c (with the files inside)
The problem is I don't want directory b in the middle, just c with all its contents
This works for me. Create data
mkdir -p a/b/c
echo 42 > a/b/c/file.dat
Archive
tar zc -f c.tar.gz -C a/b c
created a/b/c directories, from directory a kindly try this command.
so the file under b/c/files were done out.tar.gz
new directory "mydir" create under "b" and files extracted too.
out.tar.gz removed from "a".
# tar -cvzf out.tar.gz b/c/* ; mkdir -p b/mydir ; tar -xvzf out.tar.gz -C b/mydir/ ; rm -rf out.tar.gz
Thanks!

Resources