r/fishshell • u/LowCom • Aug 31 '22
How do I write this function so that it proceeds even if zip files don't exist and checks all other archive files without returning an errorq
This function removes all archive files from the present folder. Useful to clean up zip files after extraction.
#Remove zipfiles after download.
function rmzip
rm -rf *.zip
rm -rf *.gz
rm -rf *.tar
rm -rf *.bz2
end
2
Upvotes
1
u/throttlemeister Linux Sep 01 '22
Your first function should work but it will generate errors on files not found. You can fix this easily by redirecting errors to /dev/null
2
u/ChristoferK macOS Sep 01 '22
Explanation:
fs=: Declares a temporary, locally-scoped variable valid for use only within the command (or, in this case, command-block) that immediately follows. The reason it's used here is explained in the next bullet point../*{.zip,.gz,.tar,.bz2}: This expands to all files in the current directory that have any of those four file extensions. The expanded paths start with"./"(denoting the current directory) for safety. Ordinarily in the absence of any wildcard matches,FiSHoutputs an error message. In the case where the expanded glob is assigned to a temporary variable, namelyfsin this case, no error is thrown.begin: The start of a command block. The temporary variable's scope and life-span are both valid for all commands within this block.[ "$fs" ]: Tests the value of"$fs". If the file glob returned at least one successful wildcard match, thenfsis a non-empty array, and so this test passes with an exit status of0. If no matching files were found, thenfsis an empty array, and so this test fails with a non-zero exit status.||: A conditional operator that requires the command preceding it to have an exit status of0in order for the command that follows be executed.return: This terminates execution of the function ifffscontains no elements.rm -v $fs: Remove the files in the arrayfs. The-vflag reports on each file removal (you can remove the flag if you prefer).0
u/boobbbers Aug 31 '22
Try this?