Remove element, which is directory, from array in bash

Discussion in 'Programming/Scripts' started by Poliman, May 21, 2018.

  1. Poliman

    Poliman Member

    I have few directories contains databases backup:
    Code:
    root@s1:/mnt/disk/backups# ls
    mysql_backup-18-05-16-02:00:01  mysql_backup-18-05-18-02:00:01  mysql_backup-18-05-20-02:00:01
    mysql_backup-18-05-17-02:00:01  mysql_backup-18-05-19-02:00:01  mysql_backup-18-05-21-02:00:01
    I have an array:
    Code:
    var=($(ls))
    which contains mentioned above few directories in /mnt/disk/backups directory. I would like to remove from disk only first directory which is first element of the array $var:
    Code:
    rm -r ${var[0]}
    but I am not sure that this is good way to do it, although it is working. I am reading that this variable can be left in ram memory. Is that right and my way is good or bad?

    PS
    I tried with unset function but without success:
    Code:
    root@s1:/mnt/disk/backups# unset ${var[@]}
    -bash: unset: `mysql_backup-18-05-16-02:00:01': not a valid identifier
    -bash: unset: `mysql_backup-18-05-17-02:00:01': not a valid identifier
    -bash: unset: `mysql_backup-18-05-18-02:00:01': not a valid identifier
    -bash: unset: `mysql_backup-18-05-19-02:00:01': not a valid identifier
    -bash: unset: `mysql_backup-18-05-20-02:00:01': not a valid identifier
    -bash: unset: `mysql_backup-18-05-21-02:00:01': not a valid identifier
     

Share This Page