Scripting : useful files operations commands

Scripting : useful files operations commands

  • mdo  DigitalBox
  •   Scripting
  •   April 17, 2025

When you need to manipulate files or folders in your scripts, the below commands can be very useful.

Some of them are specific to Haiku (and you will see, they are nice), and each command will be detailed in this article :)

Command Description
cp Copies files and directories
mv Moves or renames files and directories
rm Removes files
mkdir Creates a new directory
rmdir Removes an empty directory
ln Creates hard or symbolic links to files
ls Lists files and directories
stat Displays detailed information about a file
file Identifies the type of a file by inspecting its content
cat Displays the content of a file to standard output
touch Creates an empty file or updates the timestamp of an existing file
mktemp Creates a temporary file or directory with a unique name
trash (Haiku) Moves files to the Trash instead of deleting them permanently
listattr (Haiku) Lists the extended attributes of a file
catattr (Haiku) Displays the content of an attribute of a file
copyattr (Haiku) Copies extended file attributes from one file to another

cp

This command stands for "copy" and its aim is to copy a file from a source to a destination.

Below the "readme.txt" file is copied to your "Documents" directory :

cp readme.txt /boot/home/Documents/

You can also copy the content of a whole directory with the recursive option :

cp -R Svg Documents/

The whole "Svg" directory was copied into the "Documents" folder.

mv

The "mv" command means "move" and is used when a file or a directory need to be moved to a destination folder.

Imagine you would like to move the "Svg" folder with SVG images into the "Documents" one.

For that type :

mv Svg Documents/

rm

In case you have to remove a simple file or a whole directory, the "rm" command - which means remove -  will fit your need :)

In a Terminal type :

rm Documents/readme.txt
rm -rf Documents/HaikuArchive/

The second command above is using the recursive ("r") and force ("f") option in order to remove the directory "HaikuArchive" and all its content.

Be careful about this command, because once launched you can't go backward.

For more safety, you can use instead the "trash" command described at the end of this article.

mkdir

This command - which means make directory - lets you create a new directory.

If you would like to create a new "tmp" directory in your home folder type :

mkdir tmp

In case the directory exists you will have an error message.

In order to avoid any error message because of an existing directory, you can use the "-p" option as showcased above.

rmdir

The rmdir which stands for remove directory, is useful when you need to get rid of an empty directory.

For instance :

rmdir tmp

Please note only empty directories can be removed with this command, else you will have an error message.

ln

This command - meaning link - is commonly used on Haiku to make symbolic links on files.

For instance, the below library "libxmp.so.4" is just a link to the real library file "libxmp.so.4.6.0" :

Let's suppose you would like to create a symbolic link to your favorite StyledEdit application.

Here is how to :

ln -s /system/apps/StyledEdit ./stylededit

In the above example the link /boot/home/config/non-packaged/apps/stylededit will call the original StyledEdit application located in the system apps folder.

If you want to make such shortcut on your favorite apps, a good candidate is the /boot/home/config/non-packaged/bin/ folder as it's defined in the $PATH variable.

Please note that, if you don't use the "-s" option, a hard link will be defined instead. I wont' go into the details about hard links because most of the time symbolic links are used :)

ls

The "ls" command - which stands for list - let you list the content of a directory.

You can use the standard display or use the long display with the "-l" option :

ls
ls -l

Now what about sorting the list by date/time ?

For that use the "-t" option in addition to the "-l" option:

ls -lt

As you can see above the first listing is sorted in chronological order, while the second with the "-r" (reverse) option is in reverse chronological order.

You can also sort the files listing by size with the "S" option :

ls -lSr 

As the reverse option was also used, the biggest file (which is an audio one), will appear at the end :)

stat

If you need to display detailed information on a file like its size, the permissions or the dates of access/modification, the stat command is for you :)

In a Terminal type :

stat '01 - The Weeknd - Alone Again.flac'

If you would like to have a customized display of the information, you can use the corresponding "-c" option:

file

This command can be very useful because it will help your script determining the type of a file.

For instance below, the first file is well identified as an audio FLAC, while the second is a UTF-8 text :

file '01 - The Weeknd - Alone Again.flac'

cat

The cat command is an abbreviation for "concatenate".

It lets you display the content of a text file in the Terminal like below :

cat info.php
cat -n info.php

As you can see the "-n" option will add the line number before each line displayed.

touch

The touch command is useful when you need to update the a file without touching its content.

For instance in a Terminal :

ls -l
touch README
ls -l

As you can see, the date of the file has been changed :)

This command can be used also when you need to create a new empty file.

mktemp

This command means "make temporary" and can be used when - for some reasons - you need to create a temporary file with a unique filename.

In a Terminal type :

mktemp

The file created will be :

  • located in the /tmp directory
  • unique (ie filename is unique)
  • blank (no content)

trash

Ok, now let's talk about serious things : the Haiku specific commands !

One of them I've discovered recently is the "trash" command.

Let's suppose your desktop's trash is empty and you would like to put the "filetypes.zip" in it :

In a Terminal type :

trash /boot/home/Desktop/filetypes.zip
ls -l /boot/trash

As you can see above, the "ls" command indicate the filetypes.zip file has been moved to the Trash.

Let's verify it on the desktop :

That's correct :)

In case you need to restore the file, type :

trash --restore /boot/trash/filetypes.zip

The file will be moved to its original location.

Now, what above moving a whole directory to the trash and decide to empty the trash completely ?

For that type :

trash /boot/home/Desktop/OldScreenshots
trash --empty

The last command above confirms the Trash is now empty.

As you can see, the trash commands is providing the below features :

An handy way to restore all the files from the Trash is to type :

untrash --all

The trash command might be preferred to the "rm" one, when you need to have a confirmation from the user to remove completely the files/folders.

It's also useful when you are experimenting tasks on files, and would like to be able to restore them at some point.

listattr

Let's talk about a powerful feature of Haiku : the files attributes !

The "listattr" commands lets you list of the attributes attached to a file.

For instance, let's take one of the people file below and display its attributes :

cd people
listattr DigitalBox

As you can see, each attribute has a type (Text, String, etc), a size and a name.

The name is important because it will be used for the next command.

catattr

It's nice to have attributes, but what about displaying them ?

For that the "catattr" is the lucky man ;)

If I would like to display the email of a people contact, I just need to type :

catattr META:email DigitalBox98

As you can see, it's correct, and the "Get info" from the Desktop is providing also the list of each attribute with its value :

For your script, if you only need the raw value of the attribute without any additional formatting display, please use the "--raw" option  :

catattr --raw META:email DigitalBox98

Extra bonus : did you know that with these 2 commands, you can have the details about the mime types used by Haiku ?

For instance, if you need to identify the attributes relative to the "audio" mime type:

In a Terminal type :

listattr /system/data/mime_db/audio/

Then display the META:ATTR_INFO attribute to have the details of each fields used by the mime type :

catattr META:ATTR_INFO /system/data/mime_db/audio/

Nice isn't it ?

copyattr

If you need to copy attributes from one file to another, the corresponding command is "copyattr".

Let's suppose you have two contacts as per below, and you would like to copy the email information from DigitalBox98 to DigitalBox :

In a Terminal, type :

cd people
copyattr -n META:email DigitalBox98 DigitalBox

Now open again the DigitalBox contact on the right :

The email has been copied successfully:)

You can check the "--help" on this command for more details :

One interesting option is the "-t" one which lets you copy attributes by type, in case you need so.

Any other useful files commands you would like to share ?

You can put a comment below :)


Powered by Bludit - Hosted by Planet Hoster
© 2025 Haiku Insider