Merge and compile SCSS files recursively using shell script in Windows - node.js

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

Related

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

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.

bash script to iterate directories and create tar files

I was searching for ways to create a bash file that would iterate all the folders in a directory, and create a tar.gz file for each of those directories.
(This is used specifically for ubuntu/drupal website - but could be useful in other scenarios.)
After lots of searching, combining scripts and testing, I found the following works very well when run from within the main directory.
This might be slightly different depending on version of bash, what version of ubuntu and from where you schedule or run the bash file. (Run by typing sh createDirectoryTarFiles.sh at command line from within the parent folder.)
The echo line is not necessary - just for viewing purposes.
for D in *; do
if [ -d "${D}" ]; then
tx="${D%????}"
echo "Directory is ${D} - and name of file would be $tx"
tar -zcvf "$tx.tar.gz" "${D}"
fi
done
You can use find to search for directories between mindepth and maxdepth and then create the tars
find . -maxdepth 1 -mindepth 1 -type d -exec tar czf $(basename {}).tar.gz {} \;

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

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 ..

Prevent deletion of .git folder using terminal

I want to delete all the folders and files from my local repo folder, but not the .git folder and any other folder/file proceeding with a '.'.
How exactly do I do that?
I'm running Ubuntu 14.04
Normally, the following works:
git rm -r *
That leaves the .gitignore file and any other . files unaffected. By default, in bash (and probably other shells) * does not match directories or files starting with a ..
Similarly,
rm * -r -i
will only remove files and directories not starting with a . with. (remove the -i for non-interactive mode)
The .git folder isn't itself version controlled, and Git will not remove it.
Just run git rm -r . and you'll remove all files in the repository without removing the .git directory.
try this:
find . -type f -not -name ".*" -exec rm -f {} \;
That will delete all files that aren't preceded with at "."
Please let me know if you have any questions!

Resources