If there are pictures in folder A, B, C.....Z, how do I automatically set the first picture in each of these folders as its folder icon? Is there a way like a script or something else?
3 Answers
1. Automatically change folder icon into the first found image inside
The python script below will change the icon of all folders inside a directory (recursively) into the first found valid image file inside the folder.
The script
#!/usr/bin/env python3
import subprocess
import os
import sys
# --- set the list of valid extensions below (lowercase)
# --- use quotes, *don't* include the dot!
ext = ["jpg", "jpeg", "png", "gif","icns", "ico"]
# ---
dr = sys.argv[1]
for root, dirs, files in os.walk(dr):
for directory in dirs:
folder = os.path.join(root, directory)
try:
first = min(p for p in os.listdir(folder)
if p.split(".")[-1].lower() in ext)
except ValueError:
pass
else:
subprocess.Popen([
"gvfs-set-attribute", "-t", "string",
os.path.abspath(folder), "metadata::custom-icon",
"file://"+os.path.abspath(os.path.join(folder, first))
])
How to use
- Copy the script into an empty file, save it as
change_icon.py - In the head of the script, edit, if you like, the list of extensions to be used as valid icon images.
Run it with the targeted directory as an argument:
python3 /path/to/change_icon.py <targeted_directory>
That's it!
2. More advanced
...is to make it a right-click option in nautilus:
The script is slightly different then:
#!/usr/bin/env python3
import subprocess
import os
# --- set the list of valid extensions below (lowercase)
# --- use quotes, *don't* include the dot!
ext = ["jpg", "jpeg", "png", "gif", "icns", "ico"]
# ---
# retrieve the path of the targeted folder
current = os.getenv("NAUTILUS_SCRIPT_CURRENT_URI").replace("file://", "").replace("%20", " ")
dr = os.path.realpath(current)
for root, dirs, files in os.walk(dr):
for directory in dirs:
folder = os.path.join(root, directory)
try:
first = min(p for p in os.listdir(folder)
if p.split(".")[-1].lower() in ext)
except ValueError:
pass
else:
subprocess.Popen([
"gvfs-set-attribute", "-t", "string",
os.path.abspath(folder), "metadata::custom-icon",
"file://"+os.path.abspath(os.path.join(folder, first))
])
To use
Create, if it doesn't exist yet, the directory
~/.local/share/nautilus/scriptsCopy the script into an empty file, save it in
~/.local/share/nautilus/scriptsasset_foldericons(no extension!), and make it executable.- Log out and back in, it works.
Notes
- This will change the icon of all folders inside the right-clicked folder, not of the folder itself.
- Since
os.path.realpath()is used, this also works if the targeted folder is a link.
EDIT
Undo (reset) the custom icons inside a directory recursively
If, for some reason you'd like to reset the icons inside a folder to their default icon(s), use the script below. Simply:
- copy it into an empty file, save it as
reset_icons.py run it by the command:
python3 /path/to/reset_icons.py <target_directory>
The script
#!/usr/bin/env python3
import subprocess
import os
import sys
dr = sys.argv[1]
for root, dirs, files in os.walk(dr):
for directory in dirs:
folder = os.path.join(root, directory)
subprocess.Popen([
"gvfs-set-attribute", os.path.abspath(folder),
"-t", "unset", "metadata::custom-icon"
])
- 8,628
- 11
- 55
- 93
- 82,471
- 12
- 195
- 299
-
I found that I had to input the full path, such as "/home/user/folderName" – JulianLai Jul 09 '16 at 06:28
-
If they add this function to Nautilus, it will be a better file manager. – JulianLai Jul 09 '16 at 06:43
-
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/42239/discussion-between-jacob-vlijm-and-julianlai). – Jacob Vlijm Jul 09 '16 at 09:49
-
Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackexchange.com/rooms/42239/discussion-between-jacob-vlijm-and-julianlai). – terdon Jul 15 '16 at 10:39
-
@JacobVlijm instead of first image in the folder can I make it to choose a unique file, for example `.folder.png` in every folder. this is really helpful for the directories with more than one image. I've been using this script and that's one improvement that I'd love to see in it https://askubuntu.com/questions/900785/how-to-set-folder-icons-of-multiple-folders-automatically-with-a-unique-file – Sumeet Deshmukh Apr 30 '17 at 16:10
-
First script error message: `/usr/bin/env: ‘python3\r’: No such file or directory` but python3 is installed. Is this supposed to work with all gui file browsers? – Lew Rockwell Fan May 21 '17 at 18:30
-
Nope. I use geany. I did invoke it without the initial "python3 ". Guess I assumed it was redundant with the shebang. I just noticed that. But invoking it like in the post, I still get an error, albeit a different one. First error is "set_folder_icon.py", line 25, in
"file://"+os.path.abspath(os.path.join(folder, first))" – Lew Rockwell Fan May 22 '17 at 00:41 -
@LewRockwellFan See https://askubuntu.com/questions/896860/usr-bin-env-python3-r-no-such-file-or-directory – Jacob Vlijm May 22 '17 at 05:29
-
Because of this one [this one](https://askubuntu.com/a/1090433/344926) – Fabby Nov 20 '18 at 22:28
"gvfs-set-attribute" in script 1. will make error in python3 Ubuntu 18.0.4
This tool has been deprecated, use 'gio set' instead.
I change to
"gio", "set"
bash version to look for the files named cover.jpg/cover.png under the current path and set them as a folder icons for the container folder:
IFS="\n" find . -iname "cover.jpg" -o -iname "cover.png" | read COVER; do \
gio set -t string "$(realpath "$(dirname "$COVER")")" metadata::custom-icon "file://$(realpath "$COVER")"; \
done
- 1
