1

I am trying to make a simple program in c that enters a specific folder I made in my home directory, clears the terminal screen and lists what's in the folder:

#include <stdlib.h>

void main()
{
  system("cd");
  system("cd Desktop");
  system("clear");
  system("ls");
}

When I compile and run it, it lists everything in my home directory an doesn't move to the folder I specified. I am trying to make a script I can run in my terminal from anywhere to get to my folder dedicated for work (I'm lazy), so I would also like to know how to make it a command in my terminal (like you type cd in no mater what directory you are and it does what you want it to do).

Sorry for putting two questions here, but they feel somewhat related.

Mathieu
  • 8,840
  • 7
  • 32
  • 45
someoneb100
  • 141
  • 1
  • 13
  • 3
    Each `system` invocation runs in a seperate child process and each is independent. Any changes to the environment from one `system` call does not affect the original process nor any other subsequent `system` invocations. You are better off writing an actual bash script rather than a C program like this. – kaylum May 17 '16 at 11:18
  • @kaylum do you know how i could execute a bash script in a c program (both are in the same directory for simplicity)? – someoneb100 May 17 '16 at 12:19

3 Answers3

2

system runs in a separate process and any environment changes (like the change of the current directory) are discarded when the process ends.

Since you want to do several commands you could put them in a shell script.

Or you could run something that does the ls in a single call to system:

system("clear");
system("/bin/sh ls $HOME/Desktop");

I use sh to run ls to get access to shell expansion of $HOME.

As for your second question:

To make your program run as a command it needs to be in a directory that is listed in the PATH. You can either put your program in an existing directory in the PATH or you can add the directory where your program is to the PATH.

If you plan on creating a set of personal utilities I recommend that you create a directory $HOME/bin and copy your programs there. Then add the following to your login script (should be something like $HOME/.Login or $HOME/.bashrc):

export PATH="$PATH:$HOME/bin"
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • when i do the export command he stops responding to basic bash (like ls) and i have to close the terminal and re-open it and everything is back to normal. I tried making bash scripts in the /usr/bin folder, but it says i dont have permission and sudo doesn't work. Could you help me with that as well? – someoneb100 May 17 '16 at 12:41
  • @someoneb100 You typically don't want to place your own stuff in `/usr/bin`. Strange that the `export` statement isn't working for you. – Klas Lindbäck May 17 '16 at 14:21
1

You cannot use system() that way: system() will create a sub-process in which the specified command will be executed.

So if you execute system("cd"), the sub-process created will have its current directory changed, but parent program (your code) won't know it.

To make what you want, use chdir.

Mathieu
  • 8,840
  • 7
  • 32
  • 45
0

You can just add an alias to your ~/.bashrc file:

alias wcd='cd /dedicated/dir/path'

Now, from anywhere in your terminal, just run wcd to move to your directory.

If you want to run ls too then:

alias wcd='cd /dedicated/dir/path && ls'

Note: Open ~/.bashrc file and add the above line at the end, Then open a new terminal or run source ~/.bashrc in existing terminal window.

Jahid
  • 21,542
  • 10
  • 90
  • 108