Prevent deletion of .git folder using terminal - linux

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!

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

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 command to delete all files except .git folder?

I want to delete all of the current directory's content except for the .git/ folder before I copy the new files into the branch.
What's the linux command for that?
Resetting the index is cheap, so
git rm -rf .
git clean -fxd
Then you can reset the index (with git reset) or go straight on to checking out a new branch.
With find and prune option.
find . -path ./.git -prune -o -exec rm -rf {} \; 2> /dev/null
Edit: For two directories .git and dist
find . -path ./.git -prune -o \( \! -path ./dist \) -exec rm -rf {} \; 2> /dev/null
As Crayon mentioned in the comments, the easy solution would be to just move .git out of the directory, delete everything, and then move it back in. But if you want to do it the fancy way, find has got your back:
find -not -path "./.git/*" -not -name ".git" | grep git
find -not -path "./.git/*" -not -name ".git" -delete
The first line I put in there because with find, I always want to double-check to make sure it's finding what I think it is, before running the -delete.
Edit: Added -not -name ".git", which keeps it from trying to delete the .git directory, and suppresses the errors. Depending on the order find tries to delete things, it may fail on non-empty directories.
One way is to use rm -rf *, which will delete all files from the folder except the dotfiles and dotfolders like .git. You can then delete the dotfiles and dotfolders one by one, so that you don't miss out on important dotfiles like .gitignore, .gitattributes later.
Another approach would be to move your .git folder out of the directory and then going back and deleting all the contents of the folder and moving the .git folder back.
mv .git/ ../
cd ..
rm -rf folder/*
mv .git/ folder/
cd folder
for i in `ls | grep -v ".git"` ; do rm -rf $i; done; rm .gitignore;
the additional rm at the end will remove the special .gitignore. Take that off if you do need the file.
as CB Bailey mention:
I want to remove the history of tracker files too.
git rm -rf .
git clean -fxd
git update-ref -d refs/heads/master #or main or ...
should find all the files and directories that with the name .git
find . -name .git
should find all the file and directories not named .git
find . -not -name .git
delete all the files that you find
find . -not -name .git -exec rm -vf {} \;
be sure that the find is doing what you want
if you want to delete directories change the rm command to rm -rvf
I include the v option to see the files that are deleted.
if you want to make sure about the files before you delete them
pipe the find command to a file and review the results

cp -r without hidden files

I have two directories and one is empty.
The first directory has many sub directories with hidden files. When I cp -r content from first directory to the second one, the hidden files gets copied too. Any solutions to escape them?
You can use rsync instead of cp:
rsync -av --exclude=".*" src dest
This excludes hidden files and directories. If you only want to exclude hidden directories, add a slash to the pattern:
rsync -av --exclude=".*/" src dest
You can do
cp -r SRC_DIR/* DEST_DIR
to exclude all .files and .dirs in the SRC_DIR level, but still it would copy any hidden files in the next level of sub-directories.
rsync has "-C" option
http://rsync.samba.org/ftp/rsync/rsync.html
Example:
rsync -vazC dir1 dir2
I came across the same need when I wanted to copy the files contained in a git repo, but excluding the .git folder, while using git bash.
If you don't have access to rsync, you can replicate the behavior of --exclude=".*" by using the find command along with xargs:
find ./src_dir -type f -not -path '*/.*' | xargs cp --parents -t ./dest_dir
To give more details:
find ./src_dir -type f -not -path '*/.*' will find all files in src_dir excluding the ones where the path contain a . at the beginning of a file or folder.
xargs cp --parents -t ./dest_dir will copy the files found to dest_dir, recreating the folder hierarchy thanks to the --parents argument.
Note: This will not copy empty folders. And will effectively exclude all hidden files and folders from being copied.
Link to relevant doc:
https://linux.die.net/man/1/cp
https://linux.die.net/man/1/find

Resources