Zip files containing only spaces and no extension - zip

I have been using to zip up some icon files.
But for a project I needed to rename some .png files to filenames using only spaces and no extension.
zip -u icons.zip *.*
But now it does not zip those files?
Any fix?

Matches any number of characters. You can use the asterisk (*) anywhere in a character string.
zip -u icons.zip *

Related

How to compress multiple folders with certain name?

I have the following folder,
(Project) [Usr#hpc FOB]$ ls
exec_train.sh FOB_RE2250_BS4ES025.py network_checkpoint_FOB_RE2250_BS2ES05
FOB_RE1150.py FOB_RE2250_BS4ES05.py network_checkpoint_FOB_RE2250_BS2ES1
FOB_RE1200.py FOB_RE2250_BS4ES1.py network_checkpoint_FOB_RE2250_BS4ES025
FOB_RE2250_BS05ES1.py FOB_RE2250.py network_checkpoint_FOB_RE2250_BS4ES05
FOB_RE2250_BS05ES2.py FOB_RE50.py network_checkpoint_FOB_RE2250_BS4ES1
FOB_RE2250_BS1ES1.py network_checkpoint_FOB_RE2250_BS05ES1
FOB_RE2250_BS2ES05.py network_checkpoint_FOB_RE2250_BS05ES2
FOB_RE2250_BS2ES1.py network_checkpoint_FOB_RE2250_BS1ES1
How do I compress the all the network_checkpoint_FOB.... into one .tar.gz archive?
I know I could manually use $ tar -czf FOB.tar.gz network_checkpoint_FOB_RE2250_BS1ES1 network_checkpoint_FOB_RE2250_BS05ES1 ... but this seams cumbersome. I think there should be a way to use string matching but I haven't been able to find a clear concise solution.
You can use wildcard character * in Bash:
$ tar -czf FOB.tar.gz network_checkpoint_FOB*
Bash automatically expands network_checkpoint_FOB* expression to space separated matching file/folder names.

How to specify the tar final structure

I have this structure:
release/folder1/file1
release/folder2/file2
...
release/folderN/fileN
I want to include all those folders (folder1, folder2 ... folderN) in a tar file.
The key is that I want these folders to be in the final tar within another directory named MYAPP so when you open the tar you can see this:
MYAPP/folder1/file1
MYAPP/folder2/file2
...
MYAPP/folderN/fileN
How can I achieve this without renaming the original "release" directory and/or creating new directories.
Is this possible to achive just in the tar process?
Thanks
Add
--transform=s#^release/#MYAPP/#
to your tar command line.
The argument of the --transform command line is a command that is passed to sed together with the file path before it is stored in the archive (use tar -tf to show the names of the files stored in the archive).
The command s#^release/#MYAPP/# tells sed to search (s) release/ at the beginning of the string (^) and replace it with MYAPP/.
The / at the end of the search and replace strings is needed to be sure the complete name of the component is release (to not replace release.txt). The # character is just a regex delimiter. Usually / is used as a regex delimiter but we prefer to use a different delimiter here to avoid the need to escape / (because it is used in the search and replace strings).
Read more in the documentation of tar and sed.

Automatically renames files with correct file extention in bulk

I have a folder with multiple types of file ( mp4, mp4, jpg, wma .etc) and these files have either have no extension, or all messed up extensions extension such as mp3.mp3, mp3.jpg, or just file name. I was reading that exiftool or even python magic can be used to assign correct file extension on understanding filetype. I am looking for exiftool based solution where these file can be renamed with correct file extension.
eg
filename (this is mp3 file)
filename1.jpg ( this is again mp3 file, with jpg as file extension)
filename.mp3.mp3.mp3 (repetition of extension)
At the simplest, try this (change double quotes to single quotes if on Mac/Linux):
exiftool -ext "*" "-filename<$filename.$filetype" TargetDir
or
exiftool -ext "*" "-testname<%f.$filetype" TargetDir
That will simply add the extension all the files in TargetDir. To recurse, add -r. If there was already an extension, this will add the proper extension at the end of the false extension e.g. filename.mp3 would become filename.mp3.jpeg.
For a more complex version which strips away some of the previous, false extensions, you could try something like this:
exiftool -ext "*" "-filename<${filename;s/(\.(mp3|mp4|jpe?g|png|wma|mov))*($)//i}%-c.$filetype" TargetDir
which would strip away extensions that are in the center parens in the regex. The %-c will add a number if the resulting rename would be a duplicate e.g. filename.jpeg, filename-1.jpeg, … filename-n.jpeg.
Edit: added -ext option to deal with files without an extension.

Unix Shell - Zip command

How do I use command zip so that i can compress specific files and other files that start with "red". They're all in the same directory
I'm trying zip myfiles bookyhs.txt sholah.txt ^"red"
having problems with the last part of the code
You need to use glob pattern instead of regex pattern to match all files starting with red in current directory:
zip myfiles bookyhs.txt sholah.txt red*

Find all PHP files in the current folder that contain a string

How could I show names of all PHP files in the current folder that contain the string "Form.new" in a Linux system?
I have tried grep "Form.new" .
You need to search recursive or using* instead of ., depending of whether you want to search only file right inside that directory or also in deeper levels. So:
grep -r "Form\.new" .
or
grep "Form\.new" *
Assuming that your PHP files have a .php extension, the following will do the trick:
grep "Form\.new" *.php
Like #LaughDonor mentioned, it's good practise to escape the dot; otherwise, dot is interpreted as “any character” by grep. "Form.new" also matches "Form_new", "Form-new", "Form:new", "FormAnew", etc.

Resources