Diff using filenames only - diff

Is it possible using any sort of diff utility to diff based on filename only, ignoring all folders and subfolders?
So if I have
/folder_1/a/1243.txt
and
/folder_2/b/1243.txt
or
/folder_2/1234.txt
It would match the files when doing a diff between folder_1 and folder_2?

Are you trying to do a diff on lists of filenames, to see which filenames one folder contains that the other doesn't? If so, do find folder_1 -type f in Linux or dir /s /b /ad folder_1 in DOS and pipe the output into text files, then diff the contents of the text files.
In Vim I'd skip the temp files, and do :.!find folder_1 -type f in one window, :.!find folder_2 -type f in second window, then :windo diffthis to diff them.

diff -r directory another
compares the files in directories and outputs if they differs, or is missing from either of the directory.

Related

Git Bash find exec recursively on folders and files containing spaces

Question: In Git Bash on windows, how would you run the following in a way that it will also search folders with spaces in the name, and execute on files with spaces in the name?
$ find ./ -type f -name '*.png' -exec sh -c 'cwebp -q 75 $1 -o "${1%.png}.webp"' _ {} \;
Context I'm running Git Bash on windows, trying to execute a command on all found .png files to convert them to .webp format. It works for all files without spaces in the path, but it's failing to find files with spaces in the filename or files within folders that have spaces in the folder name.A few considerations:
I have many, many levels of folders to iterate through, and I can't run this command separately for each. I really need the recursion to work.I cannot change the folder names; it will break other dependencies (nor did I create the folder or filenames originally, so cut me some slack!)I arrived here by following the suggestions from this article: https://www.smashingmagazine.com/2018/07/converting-images-to-webp/the program, to my knowledge, doesn't ship with any built-in recursive command... golly that'd be handy
Any help you can provide will be appreciated. Thanks!

Batch add basename to another file

I screwed up with mmv and deleted a few characters from the end of the filenames in a folder (before the extension).
Luckily I have other files with the same original basename but with a different extension.
I would like to know whether there is a way make a loop:
To match the remainder of the basename from the modified file to the second file with the original filename.
If it matches, rename the file with the original filename
Example:
Wrong filenames: foo-1234.txt
foo-1225.txt
Right files: foo-1234-5678.png
foo-1225-6789.png
Desired output:
foo-1234-5678.txt
foo-1225-6789.txt
Thank you very much in advance!
You didn't mention what environment we are talking about, but I'm assuming it's Windows cmd. You can try this:
#echo off
setlocal enabledelayedexpansion
for %%f in (foo-*.png) do set f=%%f & rename !f:~0,8!.txt !f:~0,13!.txt
It must be executed from batch file - delayed expansion won't work from direct command line.
EDIT
powershell counterpart:
dir foo-*.png | % { $stem = $_.name -replace 'foo-([^.]+).png','$1'; $bad = $stem.substring(0,4); mv foo-$bad.txt foo-$stem.txt; }
bash:
for file in foo-*.png; do stem=${file:4:9}; bad=${file:4:4}; mv foo-${bad}.txt foo-${stem}.txt; done

Rename and overwrite files using wildcard in Windows

I am working on a script for auto deployment, where I need to replace my files with the same filenames.
For example, I have the following files in my current directory
deployment.properties
wrapper.conf
config.properties
Later, I will generate another set of files like this
deployment.properties.tokenized
wrapper.conf.tokenized
config.properties.tokenized
Lastly, I want to replace the existing config files (in the first code block) using the *.tokenized version and remove the tokenized files.
In Linux, the following can do the job. But I don't know how to do in Windows
for f in *tokenized;
do mv "$f" "`echo $f | sed s/tokenized//`";
done
I tried to use powershell's move-item, rename-item but still cannot figure out the right way to do it. Could somebody help? bat / powershell scripts are both welcomed. Using loop is also okay. Thank you.
It is almost the same code but in cmd / batch files we have access to the elements of the file name.
From command line
for %a in (*.tokenized) do move /y "%a" "%~na"
Or, for a batch file (you need to escape the for replaceable parameter)
for %%a in (*.tokenized) do move /y "%%a" "%%~na"
As the extension of the file (the text after the last dot) is .tokenized, when you request just the name (without extension) of the file being referenced (%%~na) you get the original file name.
This PowerShell script should do the job:
Get-ChildItem *.tokenized | % {
move $_.Name ([System.IO.Path]::GetFileNameWithoutExtension($_.Name)) -Force
}

Change attributes of a group of files in MS-DOS

The MS-DOS command attrib changes the attributes of a single file. How can I use it to change the attributes of a group of files?
This is the info you need
Displays or changes file attributes.
ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [drive:][path][filename]
[/S [/D]]
+ Sets an attribute.
- Clears an attribute.
R Read-only file attribute.
A Archive file attribute.
S System file attribute.
H Hidden file attribute.
[drive:][path][filename]
Specifies a file or files for attrib to process.
/S Processes matching files in the current folder
and all subfolders.
/D Processes folders as well.
By using the '/s' parameter will do it for matching files for example
attrib -rhsa *.txt /s
That will remove the read, hidden, system and archive attributes from ALL files ending with '.txt'.
Where is this group of files?
You can use the FOR command for greater flexibility:
FOR /R "[directory]" %%f IN ([filetype]) DO (
attrib [opts] "%%f"
)
Where [directory] is a directory (like %CD% or C:\Users\me\Desktop), [filetype] is a filter (like *.txt or log?.log) and [opts] is the option set you will use to call attrib.
I believe it would be using the wildcard character. Such as:
attrib +r * --This sets all files with that attribute
or
attrib +r *.doc --sets files ending in .doc with that attribute
Or something of the sort, whichever fits your needs. However, if there are hidden files in the result, those won't be updated. At least they weren't on my computer.
EDIT: Use the previous answer for the other attributes that can be set. I only listed an example, not a complete list. www.computerhope.com is a good site for dos commands.

Command line to recursively delete files but excluding a certain file

I need to delete files of a certain type (.zip files, say) from a folder, and all of its sub-folders, using the command line. Ideally I am looking for something that can be run as a .bat file in Windows.
I know there is a /S switch for the DEL command to look in sub-folders, but to add to the challenge I need to exclude files of a certain name ("Backup.zip" as an example).
Is there a way to delete files recursively but exclude files of a certain name. It will not be practical in my situation to explicitly list all the file names I want to delete, only the files of the matching type I don't want to delete.
A nice trick: make the files you want to exclude read-only!
DEL /S will not delete read-only file.
The following script does not do exactly what you want (see my remarks below) but shows you how read-only files can be used to avoid deletion.
#ECHO OFF
:: This example supposes your first parameter is
:: the relative path to the folder to be deleted
:: and the second is the relative path (from the
:: the target folder) of the one to be excluded
:: Notice that this will only work if the folders
:: are in the working drive, if not, you ll
:: have to specify full paths
IF "%1"=="" GOTO ERROR
IF "%2"=="" GOTO ERROR
IF NOT EXIST %1\NUL GOTO ERROR
CD %1
IF NOT EXIST %2\NUL GOTO ERROR
ECHO Starting up the deletion process
ECHO. * Setting attributes
attrib %1\*.mp3 -r -s -h > NUL
attrib %2\*.mp3 +r > NUL
ECHO. * Deleting files
del /s %1\*.mp3
ECHO. * Reseting attributes
attrib %2\*.mp3 -r > NUL
ECHO.
ECHO Operation completed!
ECHO.
GOTO END
:ERROR
ECHO Parameters:
ECHO. Param1 -> target folder
ECHO. Param2 -> folder to be ignored
ECHO.
GOTO END
:END
Note: you can adapt this script in order to ignore not just a sub-folder but all files of given type:
attrib /S *.xxx +r > NUL
will in effect help you to exclude all 'xxx' files of the current directory and all sub-directories (hence the /S option).
Note: the "> NUL" part is a redirection often used to hide standard output, instead of displaying it on screen.
It can be dangerous if used too often (in a large loop with different paths involved, for instance) since it is a device, and like all devices (AUX, COMn, LPTn, NUL and PRN):
opening a device will claim one file handle. However, unlike files, devices will never be closed until reboot.
each device exists in every directory on every drive, so if you used redirection to NUL in, say, C:\ and after that you use it again in C:\TEMP, you'll lose another file handle.
Just do this, easy
windows button + r
type cmd and hit enter
Navigate to parent directory:
type c: or d: (or letter of the drive you want to navigate to)
type dir to see a list of that directory's contents ( dir /ah to see hidden files )
then to change directory, type cd xxxx ( xxxx = directory name )
Repeat 4&5 until you get to the directory where you want to run the batch delete
then type your pattern. Something like: del /S /ah *.jpg and hit enter. It will run through all sub-directories, and remove all visible and hidden files that are .jpg files
* is a wildcard
/S deletes from all subfolders
/ah a = select files based on attribute, h = hidden
Example: to delete those annoying .DS_Store files that appear when you copy from Mac to Windows, run:
del /S /ah .DS_Store
or
del /S /ah ._*
which will get all the 'duplicate' hidden files that are also created when copying from Mac to Windows.
You can easily loop a set of files and perform a command on each one, like this:
set match=D:\blah\M*.zip
for %%x in (%match%) do (
del %%x
)
Then I think you need to read this article on how to manipulate strings in DOS:
http://www.dostips.com/DtTipsStringManipulation.php
You can simply use below:
forfiles /p C:\temp-new /s /c "cmd /c if #isdir==FALSE del #file"
TechNet Referenceenter link description here
Perhaps the 'forfiles' command could be of use
http://technet.microsoft.com/en-us/library/cc753551.aspx
Hope that helps.
This script will delete all .zip files from a folder (and subfolders), BUT ONLY IF the file name does not contain the word "backup", or "Backup" or "BACKUP", etc.
# Script DeleteZip.txt
var str folder, filelist, file
cd $folder
lf -r -n "*.zip" > $filelist
while ($filelist <> "")
do
lex "1" $filelist > $file
# Does this $file contain the word "backup" ? Do case independent search (-c option).
if ( { sen -c "^backup^" $file } <= 0 )
system del ("\""+$file+"\"")
endif
done
Script is in biterscripting ( http://www.biterscripting.com ). Save the script in file C:/Scripts/DeleteZip.txt. Run the script as
script "C:/Scripts/DeleteZip.txt" folder("C:/testfolder")
Will do the delete operation on folder C:/testfolder and all its subfolders, at all levels.
A simple way that you need:
FORFILES /P "DIRECTORY" /S /M SEARCHMASK /C "cmd /c if #file neq \"FILE_TO_EXCLUDE\" del /f #file"
You still can adapt the command to your need to exclude specific files and/or by date. More details in the 'forfiles' documentation below.
https://learn.microsoft.com/pt-br/windows-server/administration/windows-commands/forfiles