How to have "make clean" ignore folders when deleting - unix

I was given a Makefile for an assignment that gives me a make clean
command. With the way the repository is set up, it deletes everything in the /bin and /out folders, except for a file called .gitignore. This is what the command looks like:
clean:
find out/ ! -name .gitignore -type f -delete && \
find bin/ ! -name .gitignore -type f -delete
Now that I'm doing my project, I need to store things in a folder called /bin/fonts and /bin/word_lists. I'm trying to modify the command so that it ignores these two files. The only problem is, I don't know what language these commands are written in, so I don't even know where to start looking at the syntax. Could somebody point me in the right direction? I tried something like this:
clean:
find out/ ! -name .gitignore -type f -delete && \
find bin/ ! -name .gitignore ! -name fonts/FreeSans.ttf -type f -delete
But it still deletes everything in fonts, and even if it did work the way I wanted, that doesn't really solve the problem of saving every single font in the folder.
I also tried this:
clean:
find out/ ! -name .gitignore -type f -delete && \
find ./bin -mindepth 1 ! -name .gitignore ! -regex '^./fonts/\(/.*\)?' ! -regex '^./word_lists/\(/.*\)?' -delete
following this post, but it instead deleted everything INCLUDING the folders bin/fonts as well as bin/word_lists.
Any help would be greatly appreciated!

-name does not examine the full file path, it only matches against the file name (so -name FreeSans.ttf would match, but match this file name in any directory).
The predicate you are looking for is called -path but then you need to specify a pattern for the entire path.
clean:
find out/ bin/ ! -name .gitignore ! -path 'bin/fonts/*
! -path 'bin/word_lists/*' -type f -delete
(Notice also how I condensed the find to traverse two directories at the same time. I assume you mean bin not /bin; perhaps see also Difference between ./ and ~/)

Related

Unix shell scripting - Copy files alone from parent and sub-folders into a new folder

I have a parent folder with files and many sub-folders with files. I need to copy files alone from parent and sub-folders to an OutputFolder. Below is the folder structure.
ParentFolder: Parent_1.txt, Parent_2.txt
SubFolder1: Folder1_1.txt, Folder1_2.txt
SubFolder2: Folder2_1.txt, Folder2_2.txt
OutputFolder:
Parent_1.txt, Parent_2.txt, Folder1_1.txt, Folder1_2.txt, Folder2_1.txt, Folder2_2.txt
I tried below code, but it copies all the files from sub-folders to parent folder and then move to an OutputFolder. Also, when I call "sh Filename.sh", I get missing argument to `-exec'
cp: cannot stat '20190105'$'\r''/*': No such file or directory.
Today=$(date +%Y%m%d -d "today")
mkdir $Today
Yesterday=$(date +%Y%m%d -d "yesterday")
find $Yesterday -iname "*.txt" -exec cp {} $Yesterday \;
cp $Yesterday/* $Today/
Request your help on this!
I need to copy files alone from parent and sub-folders to an OutputFolder.
I tried below code, but it copies all the files from sub-folders to parent folder
In order to copy the files directly to the OutputFolder $Today, just specify $Today rather than $Yesterday after -exec cp {}.
I get missing argument to `-exec' cp: cannot stat '20190105'$'\r''/*': No such file or directory.
The \r is a sign of Windows line endings in your script - remove the CRs or save it in Unix format.
Use this:
find . -maxdepth 1 -iname "*.txt" -exec cp "{}" $Yesterday \;
to limit the depth to current directory. Mind the quotation marks around curly brackets.

Combine find and jar -xvf?

We know how to combine find and tar cvf.
How to combine each file using -exec on find with a command like jar -xvf?
The use case is, I need to find specific jar files (e.g. -type f foo*.jar) in a folder and then extract specific entries from each jar file that find finds: jar -xvf <file> META-INF/services
The general case seems to be that the user wants to exec a command cmd for each file that is found when cmd takes argument(s) after the file.
find -exec lets you substitute a file name anywhere in the command. As in the linked question, you can do this with by moving {} to the desired location.
find /path -name '*.jar' -exec jar -xvf {} META-INF/services \;

How to list and delete directories that are not symbolic links?

I see plenty of answers on how to list all symlinks and how to remove all symlinks within a specific directory. However what about vice versa?
How would one go about listing/removing all directories within a directory that are not symlinks?
I know that rm -R removes all directories recursively but i want to know how to make it not delete symlinks in the process.
I also know that ls lists all directories files and symlinks however i would like to know how i would go about listing only directories that are not symbolic links.
Found a way finally.
First, run:
find . -depth -type d
to make sure the output looks sane, then:
sudo find . -depth -type d -exec rm -rf '{}' \;
Sure this does get a bit messy on the console to look through, but ... it works! If anyone can find a better and cleaner way to do this please post it.

Find command in unix

I want to perform a find command in a directory, and exclude from the set of results all files that are .gif, .jpeg, and .class.
I was wondering if someone could help me out. I've been trying to play with the regex option, but clearly I'm not doing it properly.
Something like:
find . \! -name '*.class' \! -name '*.jpeg' \! -name '*.class'

Unable to move all files except directories to a folder in terminal

I have extended regexes enabled in my Bash by
shopt -s extglob
They may be useful in solving the problem.
I run the following unsuccessfully, since it moves also directories
$ mv `find . -maxdepth 1` django-tes/
I am trying to find all files except directories and move them to a directory called django-tes/.
How can you move all files except directories in a folder to a folder in terminal?
Try using find . -type f -maxdepth 1

Resources