Batch renaming files into sub directories - zsh

I have files as follows:
assets/
├─ icon_1.png
├─ icon_1#2x.png
├─ icon_1#3x.png
├─ icon_2.png
├─ icon_2#2x.png
├─ icon_2#3x.png
and I want to change it to this format:
assets/
├─ icon_1/
│ ├─ 3.0x/
│ │ ├─ icon_1.png
│ ├─ 2.0x/
│ │ ├─ icon_1.png
│ ├─ icon_1.png
├─ icon_2/
│ ├─ 3.0x/
│ │ ├─ icon_2.png
│ ├─ 2.0x/
│ │ ├─ icon_2.png
I am trying to use zmv commands such as
zmv '(*).png' $1/'$1.png'
or
zmv '(*).png' '$1/$1.png'
to get a feel of how it works. However, it tells me that I do not have the right directories. Is there a way to create directories dynamically from the file names?

I wasn't really able to find a way to parametrize the suffixes and create the directories dynamically, however I did find a solution for my specific case of the problem.
I ran the following commands on zsh:
for file in *#3x.png
do
dirname="${file%#3x.png}" #this truncates the #3x.png from 'icon_1#3x.png' and so on
mkdir "$dirname"
mkdir "$dirname/2.0x"
mkdir "$dirname/3.0x"
done
This creates the folder structures that I want. Now I only have to move the files from ./ to the desired directories as follows:
zmv '(*)#3x.png' '$1/3.0x/$1.png'
zmv '(*)#2x.png' '$1/2.0x/$1.png'
zmv '(*).png' '$1/$1.png'
I move the ones with #3x, #2x suffixes first so they don't get selected by the (*).png selector.

This can be done with zmv, but you may need a somewhat complicated pattern. This will move the files if the destination directories exist:
zmv -n '(icon_<->)(*).png' '${1:r}/${${2##}%x}${2:+.0x}/$1.png'
The -n (no-execute) option is for testing - remove it to actually move the files. The extended glob pattern <-> matches any number; it lets us split the source pattern into two pieces. The destination pattern uses several zsh parameter expansions to build the path, with repeated forward slashes being treated like a single slash for the icon_1 and icon_2 directories.
Out-of-the box, zmv will not create directories, but it will accept a custom command that can call mkdir:
mkdir_mv () {
mkdir -p -- $3:h
mv -- $2 $3
}
zmv -n -p mkdir_mv '(icon_<->)(*).png' '${1:r}/${${2##}%x}${2:+.0x}/$1.png'
The mkdir_mv code is from this answer.

Related

Sass not finding correct filepaths

I want to use sass to write individual components, for example styling a chart. But I don't want to manually include every component, but rather have one scss file that includes all the components and then gets compiled to one .css file that I can include. I found this SO answer but it didn't work for me and according to SASS, you're not supposed to use #import anymore. This is the project structure:
src/
├─ about/
│ ├─ .../
│ ├─ scss/
│ │ ├─ chart.scss/
├─ app/
│ ├─ scss/
├─ build/
│ ├─ css/
| | ├─ styles.css
├─ base/
│ ├─ styles.scss
styles.scss should include all scss files from all projects in the src, and should be compiled on every .scss save. When I do it with this command though:
sass base/styles.scss:build/css/main.css --watch --style=compressed --no-source-map
it gives the error:
Error: Can't find stylesheet to import.
╷
1 │ #use "//about/scss/chart.scss";
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
╵
base_templates\styles.scss 1:1 root stylesheet
The content of styles.scss is
#use "//about/scss/chart.scss";
and that of chart.scss:
h1 {
color: #fff;
}
(as a test). What am I doing wrong?
The path should be "/src/about/scss/chart.scss" for Sass if src is the source of the volume respectively partition. Sass never 'knows' anything about your domain or network, therefore you can't assume that you can use paths based on the webroot as absolute paths.
You can also consider using relative paths, then it would be '../about/scss/chart.scss'.

Artifactory zip directory with exclude pattern

I do store my software releases in Artifactory.
As a minimal example my repository structure looks like this:
my_repo/
├─ release-1/
├─ release-2/
├─ release-3/
│ ├─ reports/
│ │ ├─ trace.pzip
│ │ ├─ cov_report.html
│ ├─ release/
│ │ ├─ app.exe
│ ├─ debug/
│ │ ├─ app_debug.exe
Now I want to create different zipped deliveries. E.g. so that we do have an internal release package (release-3_woDebug.zip), one for the end customer (release-3_woreports.zip), etc...
my_repo/release-3/ contains all files of the release, to create the packages I want to exclude specific files.
my_repo/
├─ packages/
│ ├─ release-3-packages/
│ │ ├─ release-3_woDebug.zip
│ │ ├─ release-3_woreports.zip
├─ release-1/
├─ release-2/
├─ release-3/
Is there a simple REST or jFrog CLI command which I can use to create such zip files.
When looking at the documentation, the jf rt download (ref) command has an --exclusions option which looks already promising.
But is there a way to create such packages (zip files) without downloading the content first?

Julia `julia --project` argument use

I learned of a way to run julia, so I can use the file structure of a package for my project.
Namely julia --project inside my developement directory. With this I can load all projects structured like projectName/src/projectName.jl inside the same folder.
An Example:
all my julia projects/
├─ project 1/
│ ├─ working with files in julia.jl
│ ├─ data.csv
├─ project 2/
│ ├─ project.toml
│ ├─ src/
│ │ ├─ project 2.jl
├─ project 3/
│ ├─ draft.uxf
│ ├─ .gitignore
│ ├─ project.toml
│ ├─ auto_compile.jl
│ ├─ src/
│ │ ├─ project 3.jl
With this file structure I want to call auto_compile.jl that does the following:
using Pkg
cd("..")
Pkg.activate(".")
Pkg.instatiate()
Pkg.add("PackageCompiler")
using PackageCompiler
create_app("Project 3", "Project 3 Compiled")
However, PackageCompiler.jl only works with the --project command. The --project argument doesnt seem to modify LOAD_PATH, what does it do exactly? Can I edit my julia session with --project later on? I figured julia does the same when loading packages from the ~user/.julia/packages/ dir, but how do I do that, and are there more usefull arguments I should know about?
after some quick "testing" I can confirm:
the --project/--project=. flags when starting julia from the terminal do the same as Pkg.activate(".")

rsync include specific files and exclude directory

I have a directory structure as below:
base
├── config.yml
├── build
│ └── output.yml
│ └── <multiple level sub-directories each having many files including *.c and *.h files>
├── <source directories - having many sub-directories with various files including *.c and *.h files>
│ ├── <xyz>
| │ ├── <x1>
| │ .
│ | └── <xy>
│ .
│ .
│ └── <abc>
├── <more directories, each having multiple files including *.c and *.h files>
I need to sync this directory to remote, but I only need *.c and *.h files. Also complete 'build' directory needs to be excluded. I am running below command:
rsync -avm --include '*/' --include='*.c' --include='*.h' --exclude='*' base "$target_loc"
This is syncing all *.c and *.h files which is desired but it also syncs *.c and *.h files from build and its sub directories
I tried
rsync -avm --include '*/' --include='*.c' --include='*.h' --exclude='build' --exclude='*' base "$target_loc". It still syncs files from build and it's sub directories.
How can I fix this?
You need to put --exclude='build' before --include '*/'. Both of these rules could apply to the "build" directory, and whichever is given first takes precedence, so to get the --exclude rule to override the --include rule, you need to list it first.
From the rsync man page, in the FILTER RULES section (with my emphasis):
As the list of files/directories to transfer is built, rsync checks
each name to be transferred against the list of include/exclude
patterns in turn, and the first matching pattern is acted on: if it is
an exclude pattern, then that file is skipped; if it is an include
pattern then that filename is not skipped; if no matching pattern is
found, then the filename is not skipped.

Symfony Folder Structure Standard

Is there someone who can explain in detail the standard used by Symfony* to name folders and files in the directory structure?
MyBundle
├─ Controller/ <-- 1) why singular?
├─ Model/ <-- 2) why singular?
├─ Resources/ <-- 3) why plural?
│ ├─ config/ <-- 4) why the "c" is lowercase?
│ ├─ translations/
│ ├─ views/ <-- 5) why the "v" is lowercase and views is plural?
│ │ └─ Default/ <-- 6) Why uppercase?
│ │ └─ my_view.html.twig <-- 7) Why lowercase and snake case?
│ └─ public/
├─ Service/ <-- 8) why singular?
└─ Tests/
This leads to another question: if I want to create a folder which contains ArchiveSection classes inside the Model folder, how should I name it?
Model/ArchiveSections/
Model/ArchiveSection/
Model/archive_sections/
* I am using Symfony 2.3.
See the docs talking about bundle directory structure.
I would say just keep in mind that the Resources directory structure must follow the standards to get some automatic registration of view paths and translations files in the kernel. The same applies to the Command directory.
For the rest it's how you want it to be, just know that the directory structure should repeat the symfony components structure to keep logic and readability (a Twig directory for creating twig extensions or functions, a Serializer directory to hold custom normalizers...).
This is relevant to have a good directory structure as it defines your classes namespace structure.
There is no strictly rules for structure and architecture directory in symfony framework. But exist best practices and standard convention.
In your case, there is nothing issue and you can do what do you want.
It seems that underscore in name directory is not very standard convention but not mandatory.
For question deal with singular or plurial it seems that is unanswered convention from symfony developers. Example : Me I name my service directory with plurial like this 'services' and all my code run perfectly with this name.

Resources